Wednesday, April 13, 2016

UVA 100-The 3n + 1 problem Solution

Unknown
#include<stdio.h>
int main()
{
    int n,m,i,k,j,c,s;
    while(scanf("%d%d",&n,&m)==2)
    {
        s=0;
        printf("%d %d ",n,m);
        if(n>m)
        {
            k=m;
            m=n;
            n=k;
        }
        for(i=n;i<=m;i++)
        {
            c=1;
            j=i;
            while(j>1)
            {
                if(j%2==0)j=j/2;
                else j=(3*j)+1;
                c++;
            }
            if (c>=s) s=c;
        }
        printf("%d\n",s);
    }
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,