uva 100 (3n+1 problem)solution

#include<stdio.h>

int main()

{

int n,m,i,o,j,ans,sum;

while(scanf(“%d%d”,&n,&m)==2 && m>0 && n>0)

{

sum=0;

printf(“%d %d “,n,m);

if(n>m)

{o=m;

m=n;

n=o;}

for(i=n;i<=m;i++)

{

ans=1;

for(j=i;j!=1;j=j)

{

if(j%2==0)

j=j/2;

else

j=3*j+1;

ans=ans+1;

}

if(ans>=sum)

sum=ans;

}

printf(“%d\n”,sum);

}

return 0;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s