694 - The Collatz Sequence

#include<stdio.h>
int main()
{
 long A,L,term,values,cases=1;
 while(scanf("%ld%ld",&A,&L)!=EOF&&A>0&&L>0)
 {
  if(A==1)
  printf(" Case %ld: A = %ld, limit = %ld, number of terms = 1\n",cases++,A,L);
  else
  {
   term=1;
   values=A;
      while(1)
   {
    if(values==1)
    {
     printf(" Case %ld: A = %ld, limit = %ld, number of terms = %ld\n",cases++,A,L,term);
        break;
    }
    else if(values%2)
    {
     values=values*3+1;
     term++;
     if(values>L)
     {
      printf(" Case %ld: A = %ld, limit = %ld, number of terms = %ld\n",cases++,A,L,term-1);
         break;
     }
    }
    else
    {
     values/=2;
     term++;
    }
   }
  }
 }
 return 0;
}

你可能感兴趣的:(sequence)