forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-100.cpp
60 lines (46 loc) · 837 Bytes
/
uva-100.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//uva-100
//the 3n + 1 problem
//Solved
#include <iostream>
using namespace std;
int main(void)
{
int i = 0, j = 0;
while ((cin >> i) && (cin >> j)) {
if (i == 0 | j == 0)
break;
int maxAnswer = 0;
if (i <= j) {
for (int k = i; k <= j; ++k) {
int temp = 0;
int x = k;
while (x != 1) {
if (x % 2 == 1)
x = 3 * x + 1;
else
x /= 2;
++temp;
}
if (maxAnswer < temp)
maxAnswer = temp;
}
}
else {
for (int k = j; k <= i; ++k) {
int temp = 0;
int x = k;
while (x != 1) {
if (x % 2 == 1)
x = 3 * x + 1;
else
x /= 2;
++temp;
}
if (maxAnswer < temp)
maxAnswer = temp;
}
}
cout << i << ' ' << j << ' ' << ++maxAnswer << endl;
}
return 0;
}