Problem Type : Ad hoc
#include<stdio.h>
int main()
{
long long int n1,n2,l,i,j,count,sum,temp,m,s,q;
while(scanf("%lld%lld",&n1,&n2)==2)
{
if(n1==0&& n2==0)
break;
if(n1>n2)
{
temp=n1;
n1=n2;
n2=temp;
}
sum=0;
q=0;
for(i=n1; i<=n2; i++)
{
count=0;
if(i==1)
{
m=i;
sum=3;
continue;
}
s=i;
for(j=0;; j++)
{
if(s==1)
{
break;
}
if(s%2==0)
{
s=s/2;
count++;
}
else if(s%2==1)
{
s=(3*s+1);
count++;
}
}
q=count;
if(q>sum)
{
sum=q;
m=i;
}
}
printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",n1,n2,m,sum);
}
return 0;
}
#include<stdio.h>
int main()
{
long long int n1,n2,l,i,j,count,sum,temp,m,s,q;
while(scanf("%lld%lld",&n1,&n2)==2)
{
if(n1==0&& n2==0)
break;
if(n1>n2)
{
temp=n1;
n1=n2;
n2=temp;
}
sum=0;
q=0;
for(i=n1; i<=n2; i++)
{
count=0;
if(i==1)
{
m=i;
sum=3;
continue;
}
s=i;
for(j=0;; j++)
{
if(s==1)
{
break;
}
if(s%2==0)
{
s=s/2;
count++;
}
else if(s%2==1)
{
s=(3*s+1);
count++;
}
}
q=count;
if(q>sum)
{
sum=q;
m=i;
}
}
printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",n1,n2,m,sum);
}
return 0;
}
No comments:
Post a Comment