Page 2 of 93

it checks

Posted: Mon Apr 29, 2002 5:43 am
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.

DUMB

Posted: Mon Apr 29, 2002 6:07 am
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

Still having problems...

Posted: Mon Apr 29, 2002 7:01 am
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;
}

Still having problems...

Posted: Mon Apr 29, 2002 7:03 am
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]

Posted: Mon Apr 29, 2002 10:22 am
by Adrian Kuegel
You miss the EOF. Try this:
while(cin>>i && cin>>j) ...

Prob 100

Posted: Mon Apr 29, 2002 9:25 pm
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]

Posted: Mon Apr 29, 2002 9:41 pm
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.

Posted: Mon Apr 29, 2002 9:46 pm
by Eric3k
Oh that explains it. Thanks alot. :D

Posted: Tue Apr 30, 2002 12:45 pm
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!

Problem 100

Posted: Fri May 03, 2002 2:57 am
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]

problem 100, time limit exceeded, dreaded 10.030s

Posted: Fri May 03, 2002 6:49 am
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]

Posted: Fri May 03, 2002 7:32 am
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.

Posted: Fri May 03, 2002 9:23 am
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.

what about this? cost 10.04s

Posted: Fri May 03, 2002 10:47 am
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]

Posted: Sat May 04, 2002 12:19 am
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