Advertisement
Guest User

last one

a guest
Nov 28th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. // uva 100
  2.  
  3. #include<stdio.h>
  4.  
  5. int main()
  6. {
  7. /* Variables used in the program */
  8. int counter, i, j, m, maxCounter;
  9.  
  10. /* Read until the end of the file */
  11. while(scanf("%d%d",&i,&j) == 2)
  12. {
  13. /* Reset the max counter */
  14. maxCounter = 0;
  15.  
  16. /* Continue processing until i reaches j */
  17. while(i<=j) {
  18. /* Reset the counter to 1*/
  19. counter = 1;
  20.  
  21. /* Assign the value of i to the temporary variable m */
  22. m = i;
  23.  
  24. /* Perform the algorithm as described in the problem statement */
  25. while( m ! = 1) {
  26. /* If m is odd, then m = 3 x m + 1 */
  27. if(m%2 == 1) {
  28. m = 3 * m + 1;
  29. /* Else if m is even, then m = m / 2 */
  30. } else {
  31. m = m/2;
  32. }
  33. /* Increment the counter */
  34. counter++;
  35. }
  36. /* If the counter is greater than the max so far, set a new max */
  37. if(counter > maxCounter) {
  38. maxCounter = counter;
  39. }
  40. maxcounter++;
  41.  
  42. }
  43. printf("%d",maxcounter);
  44. }
  45. return 0;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //10 is even....then the next one would be 5 but its odd then 5*3+1=16
  65. //10,5,16,8,4,2,1-7 cycle
  66. //9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1- 20 cycles
  67. // 20>7
  68. //this will continue to 1
  69. //maximum would be printed
  70. }
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement