BitComet 旗下网站

转到日志
相关贴吧:
acm of uva

3n+1

楼主 发表于:2008-09-15 18:47:41 [回复]

/*  problem 3n+1     

http://acm.uva.es/p/v1/100.html
虽然时间有5s,但是如果用循环的话,将会超时,因为数字有100 0000,
故当3*n+1没有几步就超出了int型范围,因此要用long long 型,
输入scanf("%lld",&n);
*/
#include<iostream>
using namespace std;
int t=0;
int fun(long long x)
{ t++;
 if(x==1) return t;
     if(x%2!=0) return fun(3*x+1);
   else return fun(x/2);
}
int main()
{
 int n,m,q,s;long long a,b,i;
 while(scanf("%lld%lld",&n,&m)!=EOF)
 {
     a=n;b=m;s=0;
     if(a>b) swap(a,b);
    for(i=a;i<=b;i++)
    {  t=0;
     q=fun(i);
     if(q>s)s=q;
    }
    printf("%lld %lld %d\n",n,m,s);
 }
 return 0;
}


心难泰,世风坏,旧时正气今何在?正义寡,人情薄,闻道虽多,茅塞不开。怪!怪!怪! 空等待,几多载,冲出重围人心快!暴雨打,狂风袭,任他折磨,此志难改。耐!耐!耐!

 

您现在还没有登录,请在登录后发贴