uva 3n+1 基础题
注意 题意输入的循序不能变 还有就是设置一个变量数组 记忆优化
#include <stdio.h> #define INF 10000000 int rem[INF]={0},max,sum; int main() { int n,a,b,i,f,s,o; rem[1]=1; while(scanf("%d%d",&a,&b)!=EOF) { max=-1; s=a; o=b; if(a>b) { a=o; b=s; } for(i=a;i<=b;i++) { n=i; sum=1; //printf("fz:%d %d %d %d\n",i,max,sum,rem[i]); while(n!=1) { //printf("%d\n",n); if(n<INF&&rem[n]) { sum+=rem[n]-1; break; }else { if(n%2)n=3*n+1; else n=n/2; sum++; } } if(i<INF)rem[i]=sum; if(max<sum) { max=sum; f=i; } //printf("%d\n",sum); } printf("%d %d %d\n",s,o,max); } return 0; }