:: ADVANCE ::

[dovelet][다중 반복문] 3*n + 1의 최대 길이 (3nplusone) 본문

Algorithm/구현 | ETC

[dovelet][다중 반복문] 3*n + 1의 최대 길이 (3nplusone)

KSJ14 2014. 9. 22. 01:01
반응형
dovelet    4 단계  다중 반복문


3*n + 1의 최대 길이 (3nplusone)


http://59.23.113.171/30stair/3nplusone/3nplusone.php?pname=3nplusone



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
27
28
29
30
31
#include <iostream>
using namespace std;
 
int func(int n, int cnt)
{
    if(n == 2)    return cnt+1;
    if(n % 2 == 0)        return func(n/2, cnt+1);
    else return func(3*n + 1, cnt+1);
}
 
int main(void)
{
    int n, m, max = 0, i, temp;
    cin >> n >> m;
 
    if(m < n)    {
        temp = m;
        m = n; 
        n = temp;
    }
 
    for(i = n; i <= m; i++)    {
        temp = func(i, 1);
        if(max < temp)    {
            max = temp;
        }
    }
 
    cout << max << endl;
    return 0;
}


반응형
Comments