xtu1142 3n + 1

这题直接做就能过,但要注意由于n在变化过程中可能超过int ,导致n为负数,永远无法回到1,结果超时,所以用long long存就可以了

#include <stdio.h>

int main (){
 __int64 max,now;
 int a,b,count,n;

 while(scanf("%d%d",&a,&b) != EOF){
  n = b - a + 1;
  now = a;
  max = a;
  count = 0;
  while(n--){
   max = max > now ? max : now;
   while(now != 1){
    if (now % 2 == 0){
     now = now / 2;
     count++;
    }
    else{
     now = 3 * now + 1;
     max = max > now ? max : now;
     count++;
    }
   }
   a++;
   now = a;
  }
  printf("%d %lld\n",count,max);
 }
 return 0;
}


 

 

你可能感兴趣的:(xtu1142 3n + 1)