分析:給了輸入整數的范圍,根據題意可以很簡單的得到以下代碼:
用到了 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;
}
輸出示范: