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

rjhadley
Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States

Post by rjhadley »

The goto statements make the code hard to read. The original code is almost identical to the while loop version I gave, except for how it handles the value of 1. The version with the goto statements is equivalent to:
[cpp]
do {
if( j % 2 > 0 ) {
j= ( 3 * j ) + 1;
x++;
} else {
j = j / 2;
x++;
} // if
} while( j > 1 );
[/cpp]
So when j = 1, the flow of the program enters the loop and j goes from 1 -> 4 -> 2 -> 1 and x = 4 when it exits the do-while loop.

In the while loop version, the condition ( j > 1 ) is false, and the program flow doesn't enter the loop, leaving x with a value of 1. (Which is the correct answer).

You probably should consult a C++ reference about namespaces. In short, they are used to control the scope of what is declared in the headers files, i.e. where and how you can use the functions, classes, objects, etc. that are declared in the headers. This is useful if you have a large amount of code, where two methods could end up with the same name (which would normally cause compile errors). For example, we can have two variables called number if we put them in different namespaces:

[cpp]
#include <iostream>

// without this we would need to be explicit and use "std::cout" instead of "cout"
using namespace std;

namespace Alpha {
int number = 2;
}; // namespace Alpha

namespace Beta {
int number = 4;
}; // namespace Beta

int
main( void )
{
cout << "Alpha: " << Alpha::number << endl;
cout << "Beta: " << Beta::number << endl;

return 0;
}
[/cpp]

And this will print out:
Alpha: 2
Beta: 4
Namespaces are kind of new, so your code will compile and work without the line [cpp]using namespace std;[/cpp] (At least with the version of the g++ compiler currently used by the judge).
Neon
New poster
Posts: 3
Joined: Sun Nov 03, 2002 6:14 pm

Post by Neon »

I am calculating the right answers. But how do I terminate the program?

What I have done is, the program keeps looping until there is input, and terminates when something like ctrl-c is pressed. Is this fine?

I am doing this, because we dont know how many times the program should loop. I usually receive the time limit exceeded error. But I think that the code is fine.

Any input is welcome.

Neon.
jasemty
New poster
Posts: 2
Joined: Sat Jan 11, 2003 11:07 am
Location: Hong Kong

[100] Should I read the input continuously?

Post by jasemty »

In my program, should I just read all lines of input first and generated all lines of output at the end?
I got "wrong answer" from the online judge system, but I found that I get correct answer when I input manually.
Thank you for your attention.
:wink:
ElSoloMar
New poster
Posts: 9
Joined: Thu Jan 09, 2003 9:42 am
Contact:

[100] Very interesting cases

Post by ElSoloMar »

12 45
12 5
1 1 <----------- :o could it 0, 1, 3, ?
2 9
2 2 <----------- :o could it 1 or 2?
70 5000

What whould be the output for lines 3 and 5?
ElSoloMar
New poster
Posts: 9
Joined: Thu Jan 09, 2003 9:42 am
Contact:

[100] I give up !! ... This is my test data!!!

Post by ElSoloMar »

:(

For this input

Code: Select all

1               1
1               2
2               2
1               3
4               4
1               5
1               10
10              1
100             200
201             210
900             1000
I obtain the following results

Code: Select all

1 1 1
1 2 2
2 2 2
1 3 8
4 4 8
1 5 8
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174
:roll: ANY SUGGESTIONS? !!!!!
Last edited by ElSoloMar on Tue Jan 14, 2003 1:19 am, edited 2 times in total.
off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india

Post by off_algos »

YES!
my program gives the following output for ur input set:

Code: Select all

1 1 1
1 2 2
2 2 2
1 3 8
4 4 3
1 5 8
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174
off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india

end of input

Post by off_algos »

the input ends with an end of file marker
in unix systems its ^d and in dos based systems its ^z
if your program terminates at this commands then your TLE is not due to problems in receivng input/output.
check if u exceed numerical limits that would make your program run n an infinite loop
eg:

Code: Select all

   while(i!=1)
{
if(i%2){i=3*i+1;}
else {i/=2;}
}
could run infinitely if i became neg(coz of exceeding limits)
ElSoloMar
New poster
Posts: 9
Joined: Thu Jan 09, 2003 9:42 am
Contact:

Post by ElSoloMar »

It seems our difference is just in the way I handle when boths bound have
the same value. :o I can see where I am wrong now: I am considering any
case ' N N ' as ' 1 N ' and because of that I get 8 from ' 3 ' :oops: . That should be
easyly fix. Thanks you very much!!!! :D
Noel
New poster
Posts: 1
Joined: Wed Jan 15, 2003 4:58 pm

100 Why? wrong answer!!?

Post by Noel »

Help! I can't find the problem?? thanks
[cpp]
/*@begin_of_source_code*/
#include<iostream>
using namespace std;
#define MAX 1000000
void main()
{
int i=0,j=0,temp,max;
while(1)
{
cin>>i>>j;
if(cin.eof() || cin.fail()) break;
if(i>MAX||j>MAX||i<=0||j<=0)
{
cerr<<endl;
break;
}
if(i>j)
{
temp=i;
i=j;
j=temp;
}
max=0;
for(int n=i;n<=j;n++)
{
temp=n;
int count=1;
if(temp==1)
{
count=1;
}
else
{
while(temp!=1)
{
if(temp%2) temp=3*temp+1;
else
temp=temp/2;
count++;
}
}
temp=count;
if(max<temp)
{
max=temp;
}
}
cout<<i<<" "<<j<<" "<<max<<endl;
}
}
/*@end_of_source_code*/[/cpp]
route
New poster
Posts: 39
Joined: Sat Dec 21, 2002 1:25 am

Re: 100 Why? wrong answer!!?

Post by route »

The values of i and j are swapped, so the order in the output is also wrong.

Example:
Input :
10 1
Output:
(should be)
10 1
(But you)
1 10
27584NX
New poster
Posts: 6
Joined: Thu Jan 16, 2003 6:36 am
Location: Brazil
Contact:

Problem 100 output

Post by 27584NX »

Hi there ElSoloMar.
As described in the algorithm, your output for
1 1
is
1 1 1
Since we would print 1 once and then finish the loop.
For the second case:
2 2
is
2 2 2
Since we would print 2 once then divide by 2 and print 1, finishing the loop.
Try a for structure like this (if you are using C):
for ( i=min; i<=max; i++)
if min = max, we will run the code only once.
Be careful with the output ( send them in the original order ) and swap them if they come with the major number second.

See ya!
Ed C
New poster
Posts: 2
Joined: Thu Jan 16, 2003 2:55 pm

100 Why WA???

Post by Ed C »

Can someone plz tell me what's wrong:

[cpp]
#include <iostream>
using namespace std;
main (void)
{
int a, i, j, il, c;
while (cin >> i >> j)
{
cout << i << " " << j << " ";

if (i > j)
{
c = i;
i = j;
j = c;
}
c = 0;
for (int n = i; n <= j; n++)
{
a = n;
il = 1;
while (a != 1)
{
if ((a % 2) != 0)
a = (3 * a + 1);
else
a = (a / 2);
il++;
}

if (c < il)
c = il;
}

cout << c;
}
return 0;
}
[/cpp]
Last edited by Ed C on Tue Jan 21, 2003 5:35 pm, edited 1 time in total.
Nick
Learning poster
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

Post by Nick »

your logic isn't incorrect, but your program's output is the one causing problems
P100 wants you to ouput not only the max cycle length,but also the two input numbers
ex : INPUT -->10 100

OUTPUT should be --->10 100 119

also,
INPUT -->100 10
OUTPUT-->100 10 119
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho »

And you forget to
cout<<c<<endl;

you used cout<<c;

which only allow one test data

And the output might not even be flushed to the console.
Ed C
New poster
Posts: 2
Joined: Thu Jan 16, 2003 2:55 pm

Thanks

Post by Ed C »

Thanks I've sorted it now.
Post Reply

Return to “Volume 1 (100-199)”