fork(1) download
  1. #include <stdio.h>
  2. #include<math.h>
  3.  
  4.  
  5. int check(int n)
  6. {
  7. int count=1;
  8. while(1)
  9. {
  10. if(n==1)
  11. {
  12. break;
  13. }
  14. else if(n!=1 && n%2!=0)
  15. {
  16. n = (3*n)+1;
  17. }
  18. else
  19. {
  20. n/=2;
  21.  
  22. }
  23. count++;
  24. }
  25. return count;
  26. }
  27.  
  28.  
  29.  
  30. int main() {
  31. int i,j;
  32. scanf("%d %d",&i,&j);
  33. int max=0;
  34. if(i<=j)
  35. {
  36. for(int start = i;start<=j;start++)
  37. {
  38. int ans = check(start);
  39. if(ans>=max)
  40. {
  41. max = ans;
  42. }
  43. }
  44. printf("%d %d %d\n",i,j,max);
  45. }
  46. else
  47. {
  48. for(int start=i;start>=j;start--)
  49. {
  50. int ans = check(start);
  51. if(ans>=max)
  52. {
  53. max = ans;
  54. }
  55. }
  56. printf("%d %d %d\n",i,j,max);
  57. }
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
Success #stdin #stdout 0s 5308KB
stdin
1 21
stdout
1 21 21