Read post
#include<stdio.h>
int main()
{
int a,b;
int tempa,tempb;
int temp,k;
int i=1,j=1;
while(scanf("%d %d",&a,&b)!=EOF)
{
if(a>b)
{
tempa=b;
tempb=a;
}
else
{
tempa=a;
tempb=b;
}
for(k=tempa;k<=tempb;k++)
{
temp=k;
while(temp!=1)
{
if(temp%2==0)
{
temp=temp/2;
j++;
}
else
{
temp=temp*3+1;
j++;
}
if(j>i) i=j;
}
j=1;
}
printf("%d %d %d\n",a,b,i);
i=1;
}
return 0;
}
把输入部分改了一下,主要是你的输入多吸收了一个字符。
标准的输入输出程序应该是这样的
#include<stdio.h>
int main()
{
int a,b;
int tempa,tempb;
int temp,k;
int i=1,j=1;
while(scanf("%d %d",&a,&b)!=EOF)
{
if(a>b)
{
tempa=b;
tempb=a;
}
else
{
tempa=a;
tempb=b;
}
for(k=tempa;k<=tempb;k++)
{
temp=k;
while(temp!=1)
{
if(temp%2==0)
{
temp=temp/2;
j++;
}
else
{
temp=temp*3+1;
j++;
}
if(j>i) i=j;
}
j=1;
}
printf("%d %d %d\n",a,b,i);
i=1;
}
return 0;
}
另外,你的程序还可以优化一下,在1 1000000范围上运算已经很慢了,几乎无法接受。
Reply
|