100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

cout<<"enter two number i,j:"; //enter two number as it's range
cout<<"maximum cycle length:"<<max<<endl; //output maximum cycle length
Don't output anything that you aren't explicitely asked to output.
If you give your program the input, given in "Sample Input" section, it must produce exactly the same output as in "Sample Output".
is enter 4 pair of value in same time??
and output the four pair of value in same time??
That's correct.

Check http://acm.uva.es/p/data/p100.c.html for a sample accepted solution.
toyman
New poster
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by toyman »

Check http://acm.uva.es/p/data/p100.c.html for a sample accepted solution.[/quote]

Your code
while (scanf("%d %d\n",&m,&n)==2){
why shoule "scanf("%d %d\n",&m,&n)==2"....

and my code...

Code: Select all

#include<iostream>
using namespace std;
int compute(int i);   //define function                 
main()
{
    int h,i,j,max,swap; //define variables                     
    while(cin>>i>>j){ 
      if (i > j){
	     swap = i;
	     i = j;
	     j = swap;
	  }
     max = compute(i);
     for(h=i+1;h<j;h++){   
        swap=compute(h);
        if(swap>max){            
            max=swap;             
        }           
     }
     cout<<i<<" "<<j<<" "<<max<<endl;  //output maximum cycle length
    }
}
int compute(int i)
{
    int count=1;     
        while(i!=1){      
            if(i%2)        
                i=3*i+1;   
            else           
                i=i/2;     
            count++;       
        }
        return count;     
    return 0;
}
My ouput is
1 10
1 10 20
100 200
100 200 125
....

--
I have poor ability in coding@@
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

Your new code is almost correct. But please note this sentence in the problem statement:
The integers i and j must appear in the output in the same order in which they appeared in the input
(Thus, output for input "200 100" should be "200 100 125".)
toyman
New poster
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by toyman »

Code: Select all

#include<iostream>
using namespace std;
int compute(int i);  //define function                 
main()
{
    int max[20],ioo[20],joo[20];//define variables                        
    int swap,x=0,times=0,k;
    int h,i=0,j=0;
    while((cin>>i>>j)){ 
       ioo[times]=i;
	   joo[times]=j;
       if (i > j){
	      swap = i;
	      i = j;
	      j = swap;
	   }
       max[times] = compute(i);
       for(h=i+1;h<j;h++){   
          swap=compute(h);
          if(swap>max[times]){            
              max[times]=swap;             
          }           
       }
       times++;
    }
    //cout<<"The output"<<endl;
    for(k=0;k<times;k++){
                cout<<ioo[k]<<" "<<joo[k]<<" "<<max[k]<<endl;  
    }
    system("pause");
    return 0;
}
int compute(int i)
{
    int count=1;     
        while(i!=1){      
            if(i%2)        
                i=3*i+1;   
            else           
                i=i/2;     
            count++;       
        }
        return count;     
    return 0;
}
Runtime Error
Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

Before crash, it ran during 4.162 seconds.
My input is
1 10
10 1
100 200
201 210
22222222222
My output is
1 10 20
10 1 20
100 200 125
201 210 89
Is what my thought match the question's answer ???
This what i want to know?
Maybe i have wrong idea for this question!!
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You got runtime error because there are more than 20 test cases in the input.

Your previous code is quite working, as I said. Just do cout<<i<<" "<<j<<" "; before swapping i and j, and change the condition of the for-loop to "h<=j":

Code: Select all

int main()
{
    int h,i,j,max,swap;
    while(cin>>i>>j){
      cout<<i<<" "<<j<<" ";     // outputs i and j in their original order
      if (i > j){
        swap = i;
        i = j;
        j = swap;
      }
      max = compute(i);
      for(h=i+1;h<=j;h++){    // checks numbers between i+1 and j inclusive
         swap=compute(h);
         if(swap>max){           
             max=swap;             
         }           
      }
      cout<<max<<endl;
    }
}
...
toyman
New poster
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by toyman »

3QQQ~~ :D
My question have solved!!!
Thank your help!! :D
diego andres de barros
New poster
Posts: 5
Joined: Mon Jul 10, 2006 3:37 pm

Post by diego andres de barros »

instead of while (scanf("%ld %ld",&i,&j)!=EOF)

the correct is while (scanf("%ld %ld",&i,&j)==2)
lck
New poster
Posts: 1
Joined: Thu Jul 27, 2006 6:46 am

100 in haskell

Post by lck »

Why it take so long to calculate the answer in haskell while only one sec in c??

Code: Select all

f 1 = 1
f n = 1 +
    if even n
        then f $ div n 2
        else f $ n * 3 + 1
longCyLn i j = maximum $ map f [i..j]
putStrLn longCyLn 1 4000
:roll:
Chiech
New poster
Posts: 2
Joined: Mon Jul 24, 2006 8:08 am
Location: Taiwan

WA in 100

Post by Chiech »

#include<iostream.h>

void main()
{
unsigned long i,i1,j,j1,cup;
long m;

do
{
cin>>i>>j;
}while(i<=0||i>1000000||j<=0||j>1000000);

i1=i;j1=j;
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:
unsigned long z;
*(a2_array)=0;

for(z=1;z<y;z++)
{
if(*(a2_array)<*(a2_array+z)){*(a2_array)=*(a2_array+z);}
}
cout<<i1<<" "<<j1<<" "<<*(a2_array)<<"\n";
delete[] a1_array;
delete[] a2_array;

}


This is my code~please help me ~I get wa~"~
jtmh
New poster
Posts: 16
Joined: Sat Jul 15, 2006 8:34 pm
Location: Taiwan

Post by jtmh »

diego andres de barros wrote:instead of while (scanf("%ld %ld",&i,&j)!=EOF)

the correct is while (scanf("%ld %ld",&i,&j)==2)
Both work, actually.
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

why time limit exeeded![100]

Post by newton »

Code: Select all


       Accepted     so    Deleted!

Last edited by newton on Tue Feb 20, 2007 1:51 pm, edited 1 time in total.
898989
Learning poster
Posts: 83
Joined: Wed Feb 01, 2006 12:59 pm
Location: (Fci-cu) Egypt
Contact:

Post by 898989 »

Salamo Aleko
One of the ways to get ac is to check on range on fly
make a function that get a cycle of a number

Then in main() procees directly on the range.....Then try to solve 371
Sleep enough after death, it is the time to work.
Mostafa Saad
alternate
New poster
Posts: 7
Joined: Fri Jul 28, 2006 4:04 pm

Post by alternate »

scanf returns an integer containing the number of items read. EOF is returned if no items can be read therefore in this case it's better to compare to the number of items you require in case only one of two are input. Although, you could argue that you know the input is valid in this case so comparing to EOF will also work.
alternate
New poster
Posts: 7
Joined: Fri Jul 28, 2006 4:04 pm

Post by alternate »

Loop between i and j for each input and update max to n when n is greater than max.
jtmh
New poster
Posts: 16
Joined: Sat Jul 15, 2006 8:34 pm
Location: Taiwan

Post by jtmh »

I agree. :)
Post Reply

Return to “Volume 1 (100-199)”