我的第一次提交的代码(OJ上显示答案错误):
#include<stdio.h>
int main()
{
int n, number = 0;
while(scanf("%d", &n)!=EOF)
{
if(n == 1)
{
printf("%d", number);
break;
}
while(n!=1)
{
if(n%2!=0)
n = 3*n + 1;
else
n = n/2;
number++;
}
printf("%d\n", number);
number = 0;
}
return 0;
}
修改后:
#include<stdio.h>
int main()
{
int n, number = 0;
while(scanf("%d", &n)!=EOF)
{
/*if(n == 1)
{
printf("%d", number);//此段代码完全多此一举,因为如果n=1就直接输出number的值0了
break;
}*/
while(n!=1)
{
if(n%2!=0)
n = 3*n + 1;
else
n = n/2;
number++;
}
printf("%d\n", number);
number = 0;
}
return 0;
}