HDOJ 1032 The 3n + 1 problem

View Code
 1 #include <iostream>   
2 #include <vector>
3 using namespace std;
4 int main()
5 {
6 int m,n;
7 while (cin>>m>>n)
8 {
9 int result=0,max,min;
10 //①此处最为关键,因为题目中并未指明输入大的在前
11 if(m<=n)
12 {
13 min=m;
14 max=n;
15 }
16 else
17 {
18 min=n;
19 max=m;
20 }
21 for(int i=min;i<=max;i++)
22 {
23 int count=0;
24 int temp=i;
25 while(temp!=1)
26 {
27 count++;
28 if(temp%2)
29 temp=3*temp+1;
30 else
31 temp=temp/2;
32 }
33 //②最终的终止未计算在内,所以需要+1
34 count++;
35 if (count>result)
36 result=count;
37 }
38 cout<<m<<" "<<n<<" "<<result<<endl;
39 }
40 }

posted on 2010-08-23 11:38  AdaByron  阅读(140)  评论(0编辑  收藏  举报

导航