博客 | Blog

3n+1 poj 1207

#include<stdio.h>
/*
* 3n+1 problem
**/
int calCycleLength(int n){
int length = 1;
while(1 != n){
length ++;
if(n % 2 == 0){
n = n/2;
}
else{
n = 3*n + 1;
}
}
return length;
}
int main(){
int i,j,n,max,tmp,end,begin;
while(scanf("%d %d",&i,&j) != EOF){
max = 0;
if(i< j){//i may be less than j
begin = i;
end = j;
}else{
begin = j;
end = i;
}
for(n = begin;n <= end;n++){ tmp = calCycleLength(n); if(tmp > max){
max = tmp;
}
}
printf("%d %d %d\n",i,j,max);//only one space
}
return 0;
}


max){ max = tmp; } } printf("%d %d %d\n",i,j,max);//only one space } return 0; } '}" class="bdshare_t bds_tools get-codes-bdshare"> 分享到   新浪微博 人人网 腾讯微博 QQ空间 更多



暂无评论 »

我要评论

(必填)

(必填,不会公布)

无觅相关文章插件,快速提升流量