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

jimbob
New poster
Posts: 5
Joined: Mon Apr 29, 2002 5:16 am
Location: Greeley, CO

it checks

Post by jimbob »

My code checks out... I get the same answers as the example input/output...What I want to know is how the judge interprets the input/output... My program takes one line at a time, carriage return, then outputs the repsonse. For some reason, it won't take with the judge.
jimbob
New poster
Posts: 5
Joined: Mon Apr 29, 2002 5:16 am
Location: Greeley, CO

DUMB

Post by jimbob »

I didn't want to look at your site for help, but I did....its always something simple that throws me off track...

thanks
jim
jimbob
New poster
Posts: 5
Joined: Mon Apr 29, 2002 5:16 am
Location: Greeley, CO

Still having problems...

Post by jimbob »

Okay, so I swapped the values in i > j...it still says my code does not solve the problem...wtf?

Here is the latest version:

#include <iostream.h>

long int cycles(long int n)
{

long int counter = 0;

if(n != 1)
{
while( n != 1)
{
if(n % 2 == 0)
n = n / 2;
else
n = n * 3 + 1;

counter++;
}
}
else
return 1;

return counter + 1;
}


Please help...

int main()
{
long int i, j;

while(cin)
{
int cycle_count = 0;
int temp;
bool flag = false;
cin >> i >> j;

if(i > j)
{
temp = i;
i = j;
j = temp;
flag = true;
}

for(long int l = i;l <= j; l++)
{
long int cycle_hold = 0;
cycle_hold = cycles(l);

if(cycle_hold > cycle_count)
cycle_count = cycle_hold;
}

if(flag)
{
temp = i;
i = j;
j = temp;
}

cout << i << ' ' << j << ' ' << cycle_count << endl;

}

return 0;
}
jimbob
New poster
Posts: 5
Joined: Mon Apr 29, 2002 5:16 am
Location: Greeley, CO

Still having problems...

Post by jimbob »

Okay, so I swapped the values in i > j...it still says my code does not solve the problem...wtf?

Here is the latest version:

[cpp]
#include <iostream.h>

long int cycles(long int n)
{

long int counter = 0;

if(n != 1)
{
while( n != 1)
{
if(n % 2 == 0)
n = n / 2;
else
n = n * 3 + 1;

counter++;
}
}
else
return 1;

return counter + 1;
}


Please help...

int main()
{
long int i, j;

while(cin)
{
int cycle_count = 0;
int temp;
bool flag = false;
cin >> i >> j;

if(i > j)
{
temp = i;
i = j;
j = temp;
flag = true;
}

for(long int l = i;l <= j; l++)
{
long int cycle_hold = 0;
cycle_hold = cycles(l);

if(cycle_hold > cycle_count)
cycle_count = cycle_hold;
}

if(flag)
{
temp = i;
i = j;
j = temp;
}

cout << i << ' ' << j << ' ' << cycle_count << endl;

}

return 0;
}
[/cpp]
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

You miss the EOF. Try this:
while(cin>>i && cin>>j) ...
Eric3k
New poster
Posts: 29
Joined: Mon Apr 29, 2002 5:22 pm

Prob 100

Post by Eric3k »

Hi,
Today I registered and I think I've got the hang of the overall process of submitting programs, etc. However, for some reason I can't get my first program to be accepted.
In my 2 attempts, it has given Wrong Answer. Nonetheless, as far as I know, the program works correctly in my pc (yet the judge marks it as WA). :oops:
Does anyone see anything wrong in my code or perhaps might be the method of input (cin), etc...?
Thank you very much!

[cpp]
#include <iostream>

using namespace std;
#define maxval(a,b) a>b?a:b


void main(){
int i,j;

while (cin>>i>>j){
int max=0;
for (int n=i;n<=j;n++){

int p=n;
int amt=1;
again:
max=maxval(max,amt);
if (p==1)continue;
else if (p%2!=0)p=3*p+1;
else p=p/2;
amt++;
goto again;

}
cout<<i<<" "<<j<<" "<<max<<endl;
}
}[/cpp]
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

If you had looked in the existing thread about this problem, you would have seen the answer to your question. You have to look for the maximum between two values, that includes the case i>j.
Eric3k
New poster
Posts: 29
Joined: Mon Apr 29, 2002 5:22 pm

Post by Eric3k »

Oh that explains it. Thanks alot. :D
fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc »

Adrian Kuegel wrote:If you had looked in the existing thread about this problem, you would have seen the answer to your question.
I totally agree with him. Please, read before post. There is a lot of information available in this board, so please take a few minutes to make a search before posting. Thank you!
Best regards,

Fernando N
Felipe Santos Pinheiro
New poster
Posts: 2
Joined: Fri May 03, 2002 2:52 am

Problem 100

Post by Felipe Santos Pinheiro »

Hi,

This is my first mensage... I try to do the problem 100. This is my sourcer, what's the problem with it?

[c]#include <stdio.h>

static int max_clycle(int i, int j)
{
register long n;
int q, max;

max = 0;

if(i == 0) i = 1;

while(j >= i)
{
n = j;
q = 1;
while(n > 1)
{
if(n % 2)
{
n *= 3;
n++;
}
else
n /= 2;
q++;
}

if(max < q) max = q;
j--;
}

return max;
}

int main()
{
int i, j;

while(scanf("%d %d", &i, &j) == 2)
{

if(i < j)
printf("%d %d %d\n", i, j, max_clycle(i, j));
else
printf("%d %d %d\n", j, i, max_clycle(j, i));
}
}[/c]
[/c]
Davidacm
New poster
Posts: 4
Joined: Fri May 03, 2002 6:43 am

problem 100, time limit exceeded, dreaded 10.030s

Post by Davidacm »

#include <fstream>
using namespace std;

void main()
{
int i[50], j[50];
int line=0;
int maxlength[50];
ifstream fin("stdin.txt");
while (!fin.eof())
{
line++;
fin>>i[line]>>j[line];
fin.ignore(1,'\n');
maxlength[line]=0;
for(int y=i[line]; y<=j[line]; y++)
{
int count=1;
int z=y;
while(z!=1)
{
if (z%2==0)
z=z/2;
else
z=z*3+1;
count++;
}
if (count>maxlength[line])
maxlength[line]=count;
}

}


ofstream fout("stdout.txt");

for (int z=1; z<=line; z++)
{
fout<<i[z]<<" "<<j[z]<<" "<<maxlength[z]<<endl;
}
}
[cpp][/cpp][cpp][/cpp]
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Two pieces of advice from me:

1) Use the formatting the board offers so that we can read your code better.

2) Don't assume that there won't be more than 50 testcases. Make the number much larger. Even better: don't use an array at all.
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

You should print the values in the same order as in the input, i. e.
if(i < j)
printf("%d %d %d\n", i, j, max_clycle(i, j));
else
printf("%d %d %d\n", i, j, max_clycle(j, i));

And it would be nice if you use existing threads of problems. If you do so, the thread appears at the top of the thread list, there is no need to make a new one.
Davidacm
New poster
Posts: 4
Joined: Fri May 03, 2002 6:43 am

what about this? cost 10.04s

Post by Davidacm »

[cpp]
#include <fstream>
using namespace std;

void main()
{
int i, j;
int maxlength;
ifstream fin("stdin.txt");

while (!fin.eof())
{
fin>>i>>j;
fin.ignore(1,'\n');

maxlength=0;
for(int y=i; y<=j; y++)
{
int count=1;
int z=y;
while(z!=1)
{
if (z%2==0)
z=z/2;
else
z=z*3+1;
count++;
}
if (count>maxlength)
maxlength=count;
}
ofstream fout("stdout.txt",ios::app);
fout<<i<<" "<<j<<" "<<maxlength<<endl;
}
}[/cpp]
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Oh, now I see. You try to use files instead of cin and cout. That's wrong. See also the very first paragraph in the howto: http://acm.uva.es/problemset/howtows.html
Post Reply

Return to “Volume 1 (100-199)”