UVa 100
今天也算是練習了一下記憶化搜索,這道題也包含很多值得注意的地方
1、? 輸入的兩個數不一定前者比后者大
2、? 變換的過程中可能會超出int范圍
3、? 為了不超時可以作下預處理
#include<iostream>#include<cstring>
#include<cstdio>
using namespace std;
#define MAXINT 1000001
int res[MAXINT];
int dfs(long long t)
{
if(t<=1) return 1;
if(t<=MAXINT && res[t]!=0) return res[t];
if(t>MAXINT)
{
if(t%2==0)
return dfs(t/2)+1;
return dfs(3*t+1)+1;
}
if(t%2==0)
res[t]=dfs(t/2)+1;
else
res[t]=dfs(t*3+1)+1;
return res[t];
}
int main()
{
int i,m,n,temp,max;
for(i=1;i<=1000000;i++) res[i]=dfs(i);
while(scanf("%d %d",&n,&m)!=EOF)
{
printf("%d %d ",n,m);
if(n>m) temp=n,n=m,m=temp;
max=0;
for(i=n;i<=m;i++)
{
if(max<res[i]) max=res[i];
}
printf("%d\n",max);
}
return 0;
}
轉載于:https://www.cnblogs.com/yu-chao/archive/2012/02/28/2372043.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: windowsphone7高级编程中提到
- 下一篇: 深入理解JavaScript系列(32)