记忆化搜索 HDU1032

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 
 6 using namespace std;
 7 
 8 int arr[7000001];
 9 
10  int f(long long int i)
11 {
12     if(i<=7000000)
13     {
14         if(arr[i]!=0)
15             return arr[i];
16         if(i%2==0)
17             return arr[i]=f(i/2)+1;
18         else
19             return arr[i]=f(i*3+1)+1;
20     }
21     else
22     {
23         if(i%2==0)
24             return f(i/2)+1;
25         else
26             return f(i*3+1)+1;
27     }
28 }
29 
30 int main()
31 {
32     int m,n;
33     memset(arr,0,sizeof(arr));
34         arr[1]=1;
35     while(scanf("%d%d",&m,&n)!=EOF)
36     {
37         int maxi=0;
38          printf("%d %d ",m,n);
39         if(m>n)
40             swap(m,n);
41         for(int i=m;i<=n;i++)
42         {
43             if(f(i)>maxi)
44                 maxi=arr[i];
45         }
46         printf("%d\n",maxi);
47     }
48     return 0;
49 }
View Code

 

posted @ 2015-07-23 10:08  相儒以沫  阅读(94)  评论(0编辑  收藏  举报