其实就是3n+1问题,多了一个限制而已,运算时数据会超过int,开始用int64,发现uva用不了,改成longlong就可以了AC
#include<stdio.h>
void fun(long long A,long long L)
{
int flag=1;
while(A!=1)
{
if(A%2!=0)
A=A*3+1;
else
A/=2;
if(A>L)
break;
flag++;
}
printf("number of terms = %d\n",flag);
}
int main()
{
long long A,L;
int count=0;
while(scanf("%lld%lld",&A,&L)!=EOF&&A!=-1&&L!=-1)
{
printf("Case %d: A = %lld, limit = %lld, ",++count,A,L);
fun(A,L);
}
return 0;
}