Background
Problems in Computer Science are often classified as belonging to acertain class of problems (e.g., NP, Unsolvable, Recursive). In thisproblem you will be analyzing a property of an algorithm whoseclassification is not known for all possible inputs.
The Problem
Consider the following algorithm:
1. input n2. print n
3. if n = 1 then STOP
4. if n is odd then
![]()
5. else
![]()
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 isprinted) for any integralinput value. Despite the simplicity of the algorithm,it is unknown whether this conjecture is true. It has been verified,however, for all integersn such that 0 < n < 1,000,000 (and, in fact,for many more numbers than this.)
Given an input n, it is possible to determinethe number of numbers printed (including the 1). For a givenn this iscalled thecycle-length of n. In the example above, the cyclelength of 22 is 16.
For any two numbers i and j you are to determine the maximum cyclelength over all numbers betweeni andj.
The Input
The input will consist of a series of pairs of integers i and j, one pair ofintegers per line. All integers will be less than 1,000,000 and greaterthan 0.
You should process all pairs of integers and for eachpair determine the maximum cycle length over all integers between andincludingi andj.
You can assume that no operation overflows a 32-bit integer.
The Output
For each pair of input integers i and j you should output i, j,and the maximum cycle length for integers between and includingi andj. These three numbersshould be separated by at least one space with all three numbers on oneline and with one line of output for each line of input. The integersi andj must appear in the output in the same order in which theyappeared in the input and should befollowed by the maximum cycle length (on the same line).
Sample Input
1 10 100 200 201 210 900 1000
S
3nample Output
1 10 20 100 200 125 201 210 89 900 1000 174 如果是奇数则此数必然增加,如果是偶数则必然减少,特别的 当最后一位数字是2,4,8,时,该数会连续缩小两倍。如果数字 依次是奇偶奇偶,则数值会每次增加0.5倍,所以不确定最大 值会是多少,当n=159487时,最大值会达到172亿,远远超出了 int 的21亿及unsigned的42亿,所以必须考虑是否溢出,推荐用 long long 型;
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#define LEN 1000000
typedef long long int nint;
nint a[LEN];
nint circle(nint n)
{
nint count=1;
while(n>1)
{
if(n<LEN&&a[n])//n<LEN即如果n>1000000时a[t],t>1000000时越栈
{
count+=a[n]-1;//利用前面计算结果
break;
}
if(n&1)
n=3*n+1;
else
n>>=1;
count++;
}
return count;
}
int main()
{
memset(a,0,sizeof(nint));
nint j,n,m;
nint i;
for(i=1;i<LEN;i++)
{
a[i]=circle(i);
}
while(scanf("%lld%lld",&n,&m)!=EOF)
{
nint max=0;nint t;
printf("%lld %lld ",n,m);
if(n>m)
{
t=n;n=m;m=t;
}
for(j=n;j<=m;j++)
{
if(a[j]>max)
max=a[j];
}
printf("%lld\n",max);
}
return 0;
}
附:n=159487 结果:
478462
239231
717694
358847
1076542
538271
1614814
807407
2422222
1211111
3633334
1816667
5450002
2725001
8175004
4087502
2043751
6131254
3065627
9196882
4598441
13795324
6897662
3448831
10346494
5173247
15519742
7759871
23279614
11639807
34919422
17459711
52379134
26189567
78568702
39284351
117853054
58926527
176779582
88389791
265169374
132584687
397754062
198877031
596631094
298315547
894946642
447473321
1342419964
671209982
335604991
1006814974
503407487
1510222462
755111231
2265333694
1132666847
3398000542
1699000271
5097000814
2548500407
7645501222
3822750611
11468251834
5734125917
17202377752
8601188876
4300594438
2150297219
6450891658
3225445829
9676337488
4838168744
2419084372
1209542186
604771093
1814313280
907156640
453578320
226789160
113394580
56697290
28348645
85045936
42522968
21261484
10630742
5315371
15946114
7973057
23919172
11959586
5979793
17939380
8969690
4484845
13454536
6727268
3363634
1681817
5045452
2522726
1261363
3784090
1892045
5676136
2838068
1419034
709517
2128552
1064276
532138
266069
798208
399104
199552
99776
49888
24944
12472
6236
3118
1559
4678
2339
7018
3509
10528
5264
2632
1316
658
329
988
494
247
742
371
1114
557
1672
836
418
209
628
314
157
472
236
118
59
178
89
268
134
67
202
101
304
152
76
38
19
58
29
88
44
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
附:输出运算过程中国超出整形的数:
#include<stdio.h>
#include<string.h>
#define LEN 1000000
typedef long long int nint;
nint a[LEN];
nint circle(nint n)
{
nint k=n;
nint count=1;
while(n>1)
{
if(n<LEN&&a[n])//n<LEN即如果n>1000000时a[t],t>1000000时越栈
{
if(a[n]==0)
{
count=0;
break;
}
}
if(n&1)
n=3*n+1;
else
n>>=1;
if(n>2147483647)
{
count=0;
break;
}
}
return count;
}
int main()
{
memset(a,1,sizeof(nint));
nint j,n,m;
nint i;
for(i=1;i<LEN;i++)
a[i]=circle(i);
for(i=1;i<LEN;i++)
{
if(a[i]==0)
printf("%lld\n",i);
}
return 0;
}