HDU 1032 The 3n + 1 problem
2015-02-26 14:54
387 查看
/* 2015年2月26日14:46:01 思路很容易想到: 第一步:求每个数的Cycle 第二步:用当前的Cycle和maxCycle比较 有些细节要注意: (1)、小心使用全局变量,不然会出现累加效果 (2)、每次输入一组数据后记得要把maxCycle清零 */ # include <stdio.h> //int maxCycle = 0; //谨慎使用全局变量,很容出错 int countCycle(int n,int count) { if(n == 1) { count++; return count; } if((n % 2) != 0) { n = 3*n + 1; count++; countCycle(n,count); } else { n = n/2; count++; countCycle(n,count); } } /*void max(int cycle) { if(maxCycle < cycle) maxCycle = cycle; }*/ int main(void) { int i,j; while(scanf("%d %d",&i, &j)!=EOF) { int currentCycle; int maxCycle; int temp = 0; getchar(); //吸收回车符。 if((i <= 0) && (j <= 0))break; while((j - i) >= 0) { currentCycle = countCycle(i,temp); //max(currentCycle); if(maxCycle < currentCycle) { maxCycle = currentCycle; } i++; } printf("%d\n",maxCycle); maxCycle = 0; } return 0; }
相关文章推荐
- HDU 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- hdu——1032——The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 && uva 100 The 3n + 1 problem
- hdu-1032-The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem (数学)
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem
- HDU-1032 The 3n + 1 problem 不要打表
- hdu 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem (数学)
- hdu 1032 The 3n + 1 problem
- HDU 1032 The 3n + 1 problem
- hdu 1032 The 3n + 1 problem(水题)