天天看點

PAT 乙級 1001 害死人不償命的(3n+1)猜想(C語言實作)

卡拉茲(Callatz)猜想:B1001

要注意的地方:

1.這個數不超過1000

2.要用選擇分支

3.最後不用\n因為會提示格式錯誤

接下來是AC代碼

#include <stdio.h>

int main(int argc, char *argv[]) {
	
	int n;
	int step = 0;
	scanf("%d",&n);   //輸入題目給出的自然數n
	while(n!=1){     //判斷是否為1 
		if(n%2==0){
			n/=2;
		} 
		else{
			n=(3*n+1)/2;
		}
		step++;     //計數器+1 
	}
	printf("%d",step); //這裡不用\n,因為提示“格式錯誤”
	return 0;
}
           

刷pat的小夥伴加油啦