fork download
  1. #include <stdio.h>
  2.  
  3. int min(int a, int b){
  4. return (a < b) ? a : b; /* if 的簡寫版本 */
  5. }
  6. int max(int a, int b){
  7. return (a < b) ? b : a;
  8. }
  9.  
  10. int main(void){
  11. int in1, in2, cycle;
  12. int i, j, c;
  13.  
  14. while(scanf("%d %d", &in1, &in2)!=EOF){ /* EOF = end of file */
  15. cycle = 0;
  16. for(i=min(in1, in2); i<=max(in1, in2); i++){
  17. j = i;
  18. c = 1; /* 數字本身算一次 */
  19. while(j > 1){
  20. if(j & 1) j = j*3 + 1; /* 邏輯運算子判斷是否為奇數 */
  21. else j /= 2;
  22. c++;
  23. }
  24. cycle = c > cycle ? c : cycle;
  25. }
  26. printf("%d %d %d\n", in1, in2, cycle);
  27. }
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 9432KB
stdin
1 10
100 200
201 210
900 1000
stdout
1 10 20
100 200 125
201 210 89
900 1000 174