Monday, May 15, 2017

uva problem 371 - Ackermann Functions


problem link

Discussion : It is simple while loop problem.you can do it easily from the description of problem.

At first try yourself,if you can't then only see the code.


#include<bits/stdc++.h>
using namespace std;
int main()
{
      long long h,l,v,cnt,i,j,m,d,temp;
      while(cin >>l>>h)
      {
         if(h==0&&l==0)
         break;
         if(h<l)
         {
            temp=h;
            h=l;
            l=temp;
         }
         d=0;
         for(i=l;i<=h;i++)
         { cnt=0;
             m=i;
             while(1)
             {
                 if(m%2==0)
                 {
                   m=m/2;
                 }
                 else
                 {
                   m=3*m+1;
                 }
                 cnt++;
                 if(m==1)
                 break;
             }
             if(cnt>d)
             {
                 d=cnt;
                 j=i;
             }

         }
         printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",l,h,j,d);
      }
      return 0;
}

No comments:

Post a Comment