-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100.c
66 lines (58 loc) · 1.14 KB
/
100.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
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
61
62
63
64
65
66
/* 17742498 100 The 3n + 1 problem Accepted ANSI C 0.000 2016-07-27 15:20:31 */
#include <stdio.h>
#define _CACHE 1
#ifdef _CACHE
#define CACHE_SIZE 0xFFFFF
int CACHE[CACHE_SIZE] = {0};
#endif
int n(int n)
{
int counter, orig_n;
counter = 0;
orig_n = n;
while(n != 1)
{
#ifdef _CACHE
if(n < CACHE_SIZE && CACHE[n] != 0)
{
counter += CACHE[n];
break;
}
#endif
++counter;
if((n & 0x1) == 0x0)
{
n >>= 0x1;
}
else
{
n = n * 3 + 1;
}
}
#ifdef _CACHE
if(orig_n < CACHE_SIZE)
CACHE[orig_n] = counter;
#endif
return counter + 1;
}
int m(int l, int h)
{
int max, result;
max = 0;
for(; l <= h; ++l)
{
result = n(l);
max = result > max ? result : max;
}
return max;
}
int main(int argc, char const *argv[])
{
int number1, number2, result;
while(scanf("%d %d", &number1, &number2) != EOF)
{
result = number1 > number2 ? m(number2, number1) : m(number1, number2);
printf("%d %d %d\n", number1, number2, result);
}
return 0;
}