hdu 1032 The 3n + 1 problem

http://acm.hdu.edu.cn/showproblem.php?pid=1032  简单模拟

View Code
 1 #include<iostream>
2 #include<cstdio>
3 #include<iostream>
4 using namespace std;
5 int getnum(long long n)
6 {
7 int num=1;
8 while(n!=1)
9 {
10 if(n&1) n=3*n+1;
11 else n/=2;
12 num++;
13 }
14 return num;
15 }
16 void solve(int a1,int b1)
17 {
18 int maxlength=-1,temp;
19 int a=min(a1,b1);
20 int b=max(a1,b1);
21 for(int i=a;i<=b;i++)
22 {
23 temp=getnum(i);
24 if(temp>maxlength) maxlength=temp;
25 }
26 cout<<a1<<" "<<b1<<" "<<maxlength<<endl;
27 }
28 int main()
29 {
30 int a,b;
31 while(cin>>a>>b)
32 {
33 //cout<<getnum(a)<<endl;
34 solve(a,b);
35 }
36 return 0;
37 }


 

posted @ 2012-03-30 10:21  keepmoving89  阅读(131)  评论(0编辑  收藏  举报