UVa Problem 100 The 3n+1 problem (3n+1 问题)

参考:https://blog.csdn.net/metaphysis/article/details/6431937

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 ll a,b;
 7 ll length(ll x)
 8 {
 9     ll l=1;
10     while (x!=1)
11     {
12         if (x%2==1)
13         {
14             x=3*x+1;
15         }
16         else
17         {
18             x=x/2;
19         }
20         l++;
21     } 
22     return l;
23 }
24 int main()
25 {
26 //    freopen("text.txt","r",stdin);
27     while (scanf("%lld %lld",&a,&b)!=EOF)
28     {
29         ll ma=0;
30         for (ll i=min(a,b);i<=max(a,b);i++)//注意输入要先排大小! 
31         {
32             ma=max(ma,length(i));
33         }
34         printf("%lld %lld %lld\n",a,b,ma);
35     }
36     
37     return 0;
38  } 

猜你喜欢

转载自www.cnblogs.com/hemeiwolong/p/9392710.html