[백준 5679] Hailstone Sequences (C#, C++) - soo:bak
작성일 :
문제 링크
설명
주어진 시작값으로 하일스톤 수열을 만들 때, 등장하는 값 중 최댓값을 구하는 문제입니다.
접근법
현재 값이 1이 될 때까지 규칙대로 다음 값을 만들며 최댓값을 갱신합니다.
모든 입력에 대해 이 과정을 반복하면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Text;
class Program {
static void Main() {
var sb = new StringBuilder();
while (true) {
var line = Console.ReadLine();
if (line == null) break;
var h = int.Parse(line);
if (h == 0) break;
var maxVal = h;
var cur = h;
while (cur != 1) {
if (cur % 2 == 0) cur /= 2;
else cur = cur * 3 + 1;
if (cur > maxVal) maxVal = cur;
}
sb.AppendLine(maxVal.ToString());
}
Console.Write(sb);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h;
while (cin >> h) {
if (h == 0) break;
int cur = h;
int maxVal = h;
while (cur != 1) {
if (cur % 2 == 0) cur /= 2;
else cur = cur * 3 + 1;
if (cur > maxVal) maxVal = cur;
}
cout << maxVal << "\n";
}
return 0;
}