[C]ACM00100 The 3n+1 problem
題目
輸入兩數 輸出兩數及兩數之間的數最大的cycle length
基本上算簡單 要小心溢位
歡迎指教~~
輸入兩數 輸出兩數及兩數之間的數最大的cycle length
基本上算簡單 要小心溢位
#include <stdio.h>
int i,j,n,c,t;
unsigned int tt;
int main(){
while(scanf("%d %d",&i,&j)==2){
t=c=0;
//n為兩數中最小的數 當n小於另一較大的數 則n+1
for(n=i>j?j:i;n<(i>j?i:j)+1;n++){
tt=n;
//計算tt(就是n)的cycle length
while(tt>1){
//如果tt不為奇數則除2 為偶數則tt=3tt+1
if(tt%2!=1)
tt/=2;
else
tt=3*tt+1;
//cycle length(即t) + 1
t++;
}
//兩數間的數最大的cycle length
//比較c & t 將較大的數存回c
if(c<t)c=t;
//計數歸零 (( t=0
t=0;
}
printf("%d %d %d\n",i,j,c+1);
}
return 0;
}
歡迎指教~~
留言
張貼留言