100 - The 3n + 1 problem
Moderator: Board moderators
i aslo change some thing i add a new line charecter after the output.....
do you change it....
Good Luck
Rocky
Code: Select all
printf ("%d %d %d\n",n1,n2,max); //"\n" after each output...
Good Luck
Rocky
-
- New poster
- Posts: 7
- Joined: Wed Jul 19, 2006 12:26 am
- Location: Calgary, Canada
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- New poster
- Posts: 7
- Joined: Wed Jul 19, 2006 12:26 am
- Location: Calgary, Canada
well i tried ur method and got ac. however when i try it on my computer with sample input i got the following output:spicy_hair wrote:
diego andres de barros
NEVER use long long int. they are evil. i changed all your "long long int" to "int" and the programworked out perfectly
gl
i disagree with u.
the only problem is tle there,
and my previous post will fix it
but ur method doesnot overcome tle
thanks
sample input:
1 10
100 200
201 210
900 1000
output on my computer:
1 0 10
100 0 200
201 0 210
900 0 1000
clearly its not the correct output. can you explain wat happened?
thanx
Perhaps, the libraries with which programs are linked on your computer don't support %lld format. You may try to use %I64d instead.
On UVa's judge %lld works fine.
On UVa's judge %lld works fine.
Last edited by mf on Thu Jul 20, 2006 12:45 am, edited 1 time in total.
-
- New poster
- Posts: 7
- Joined: Wed Jul 19, 2006 12:26 am
- Location: Calgary, Canada
-
- New poster
- Posts: 5
- Joined: Mon Jul 10, 2006 3:37 pm
100 - Wrong assumption in the problem statement
The input description says:
But, actually, there is a total of 883 occurrences where a "3n + 1" operation overflows a 32-bit unsigned integer; in particular, a maximum value of 56,991,483,520 will be reached if the original n is 704,511. Could someone correct this? Thank you.You can assume that no opperation overflows a 32-bit integer.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
-
- New poster
- Posts: 1
- Joined: Sat Jul 22, 2006 2:41 pm
- Contact:
help me! 100!
This is my code:
#include <stdio.h>
void main()
{
long i,j,k,n=0,temp,tempn=0;
while (scanf("%ld %ld",&i,&j)!=EOF)
{
if (i>j)
{
temp=i;
i=j;
j=temp;
}
for (k=i;k<=j;k++)
{
temp=k;
while (temp != 1 )
{
if (temp % 2!=0)
{
temp=temp+temp+temp+1;
}
else
{
temp=temp>>1;
}
tempn++;
}
if (tempn> n)
n=tempn;
tempn=0;
}
printf("%ld %ld %ld\n",i,j,n+1);
}
}
============================
I don't know why WA......It seems that there's nothing wrong with it..
#include <stdio.h>
void main()
{
long i,j,k,n=0,temp,tempn=0;
while (scanf("%ld %ld",&i,&j)!=EOF)
{
if (i>j)
{
temp=i;
i=j;
j=temp;
}
for (k=i;k<=j;k++)
{
temp=k;
while (temp != 1 )
{
if (temp % 2!=0)
{
temp=temp+temp+temp+1;
}
else
{
temp=temp>>1;
}
tempn++;
}
if (tempn> n)
n=tempn;
tempn=0;
}
printf("%ld %ld %ld\n",i,j,n+1);
}
}
============================
I don't know why WA......It seems that there's nothing wrong with it..
Q100~please help~
#include<iostream.h>
void main()
{
unsigned long i,j,cup;
long m;
do
{
cin>>i>>j;
}while(i<=0||i>1000000||j<=0||j>1000000);
m=j-i+1;
if(m<0)
{
m=i-j+1;
cup=i,i=j,j=cup;
}
else
{
cup=i;
}
unsigned long *a1_array=new unsigned long [j*3];
unsigned long *a2_array=new unsigned long [j*3];
unsigned long n,x,y=1;
start:
x=1,n=1;cup=i;
for(x=1;;x++)
{
*(a1_array+x)=cup;
if(cup==1)
{
*(a2_array+y)=n;
y++;
i++;
if(i>j)
{
goto end;
}
goto start;
}
else if(*(a1_array+x)%2==0)
{
n++;
cup=*(a1_array+x)/2;
continue;
}
else
{
cup=3*cup+1;
n++;
continue;
}
}
end:
i=j-m+1;
unsigned long z;
*(a2_array)=0;
for(z=1;z<y;z++)
{
if(*(a2_array)<*(a2_array+z)){*(a2_array)=*(a2_array+z);}
}
cout<<i<<"\t"<<j<<"\t"<<*(a2_array)<<"\n";
delete[] a1_array;
delete[] a2_array;
}
============================================
please help me~
void main()
{
unsigned long i,j,cup;
long m;
do
{
cin>>i>>j;
}while(i<=0||i>1000000||j<=0||j>1000000);
m=j-i+1;
if(m<0)
{
m=i-j+1;
cup=i,i=j,j=cup;
}
else
{
cup=i;
}
unsigned long *a1_array=new unsigned long [j*3];
unsigned long *a2_array=new unsigned long [j*3];
unsigned long n,x,y=1;
start:
x=1,n=1;cup=i;
for(x=1;;x++)
{
*(a1_array+x)=cup;
if(cup==1)
{
*(a2_array+y)=n;
y++;
i++;
if(i>j)
{
goto end;
}
goto start;
}
else if(*(a1_array+x)%2==0)
{
n++;
cup=*(a1_array+x)/2;
continue;
}
else
{
cup=3*cup+1;
n++;
continue;
}
}
end:
i=j-m+1;
unsigned long z;
*(a2_array)=0;
for(z=1;z<y;z++)
{
if(*(a2_array)<*(a2_array+z)){*(a2_array)=*(a2_array+z);}
}
cout<<i<<"\t"<<j<<"\t"<<*(a2_array)<<"\n";
delete[] a1_array;
delete[] a2_array;
}
============================================
please help me~
100 Wrong Answer@@ (Plz help)
this is the #100 problem "The 3n+1"
that is my code!!
i try in my computer is correct,but i upload it to Internet is Wrong Answer!
Plz indicate my bug is where!!3Q....
Then i want to ask the input
Sample Input
1 10
100 200
201 210
900 1000
is enter 4 pair of value in same time??
and output the four pair of value in same time??
Code: Select all
#include<iostream>
using namespace std;
int compute(unsigned int ,unsigned int ,unsigned int ); //define function
main()
{
unsigned int h,i,j,max; //define variables
unsigned int temp[5000];
cout<<"enter two number i,j:"; //enter two number as it's range
cin>>i>>j;
for(h=j;h>=1;h--){
temp[h]=compute(h,i,j); //store the count's times into temp[i]
}
max=temp[1]; //set max as temp[1]
for(h=1;h<=j;h++){ //for Loop
if(temp[h]>max){ //if temp[i]>max
max=temp[h]; //max =temp[i]
}
}
cout<<"maximum cycle length:"<<max<<endl; //output maximum cycle length
//system("pause");
return 0;
}
int compute(unsigned int x,unsigned int a,unsigned int b)
{
int count=1; //define variables
if(x>=a&&x<=b){ //x must be the range of a and b(same as i and j)
while(x!=1){ //do while loop untill x==1 then stop
if(x%2) //if x is odd
x=3*x+1; //x=3*x+1
else //if x is even
x=x/2; //x=x/2
count++; //count = count + 1
}
return count; //return count's value to main function
}
return 0;
}
i try in my computer is correct,but i upload it to Internet is Wrong Answer!

Then i want to ask the input
Sample Input
1 10
100 200
201 210
900 1000
is enter 4 pair of value in same time??
and output the four pair of value in same time??
Code: Select all
Code: Select all