
分析:给了输入整数的范围,根据题意可以很简单的得到以下代码:
用到了 condition?A:B 的结构 condition=Ture=1则 执行A 否则执行B
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,count;
n = count = 0;
scanf("%d",&n);
while(n != 1)
{
n = n%2 ? (3*n+1)/2 : (n/2);
count += 1;
}
printf("There are %d steps in total",count);
return 0;
}
输出示范:


网友评论