Guest User

Untitled

a guest
Aug 9th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int main()
  5. {
  6. int n;
  7. scanf("%d",&n);
  8. int i;
  9. for(i = 0; i<n; i++)
  10. {
  11.  
  12. int c,d, cc =0;
  13. scanf("%d",&c);
  14. scanf("%d",&d);
  15.  
  16. if(c == d)
  17. {
  18. printf("0\n");
  19. continue;
  20.  
  21. }
  22.  
  23. while(c>d)
  24. {
  25. //printf("\n c is greater than d with c - %d and d - %d\n",c,d);
  26. if(c % 2 == 0)
  27. c = c/2;
  28. else
  29. c = (c-1)/2;
  30. cc++;
  31. }
  32. double rr =log(c)/log(2);
  33. printf("Current value of rr is %lf\n",rr);
  34.  
  35.  
  36. while(!(rr == (int)rr))
  37. {
  38. printf("hung in loop with c = %d\n",c);
  39.  
  40. if(c % 2 == 0)
  41. c = c/2;
  42. else
  43. c = (c-1)/2;
  44. rr = log(c)/log(2);
  45. cc++;
  46.  
  47. }
  48. if(c == d)
  49. {
  50. printf("%d\n",cc);
  51. continue;
  52. }
  53. else if(d % c == 0 && c != 1)
  54. {
  55. double r = d/c;
  56. int a = (int)(log(r)/log(2));
  57. printf("%d",a+cc);
  58. continue;
  59. }
  60. else if(c == 1 && d%c == 0)
  61. {
  62.  
  63. int cou = 0, mm = 1;
  64. while(mm != d)
  65. {
  66. cou++;
  67. mm *=2;
  68. }
  69. printf("%d\n",(cou+cc));
  70. continue;
  71. }
  72.  
  73.  
  74. }
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment