100 - The 3n + 1 problem
Moderator: Board moderators
it checks
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.
Still having problems...
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;
}
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;
}
Still having problems...
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]
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]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
Prob 100
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).
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]
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).

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]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
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!Adrian Kuegel wrote:If you had looked in the existing thread about this problem, you would have seen the answer to your question.
Best regards,
Fernando N
Fernando N
-
- New poster
- Posts: 2
- Joined: Fri May 03, 2002 2:52 am
Problem 100
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]
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]
problem 100, time limit exceeded, dreaded 10.030s
#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]
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]
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
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.
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.
what about this? cost 10.04s
[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]
#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]
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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