100 - The 3n + 1 problem

Moderator: Board moderators

mklein49
New poster
Posts: 5
Joined: Mon Sep 23, 2002 1:56 am
oops
Last edited by mklein49 on Tue Sep 24, 2002 8:26 am, edited 1 time in total.

mklein49
New poster
Posts: 5
Joined: Mon Sep 23, 2002 1:56 am
Wouldn't this,
[cpp]while(!(cin.eof()))
{
cin >> i >> j;[/cpp]

workt the same as
[cpp]while(cin >> i >> j)[/cpp]

mklein49
New poster
Posts: 5
Joined: Mon Sep 23, 2002 1:56 am
Well, you were right. I made the correction and the program was accepted.

soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

..

Some problems are test a few input data. If these problem need

simple algorithm or low time complexity solution, then these

problem's run times are extremely fast. So I think that 0.000 sec

is a possible time. But I also know that some people can cheat

their code. -_-;

Whinii F.
Experienced poster
Posts: 151
Joined: Wed Aug 21, 2002 12:07 am
Location: Seoul, Korea
Contact:

As I believe..

As I know, they work differently since eof() functions (including feof() in C) return true AFTER they have REACHED the actual end of file. So, we need to access the end-of-file location by trying to read. THEN the eof() functions will return true..

Try a little code for each one, and you'll get it..[/cpp]

makefriend8
New poster
Posts: 1
Joined: Thu Aug 29, 2002 5:28 am

what's problem in my code?? (100)

@begin_of_source_code
/* @JUDGE_ID: 22306NP 100 C++ */
#include<iostream.h>
unsigned int i,j;
unsigned int index[1000001];
int cacu(unsigned int k)
{
unsigned int s=k;
if(s==1)return 1;
int num=0;
while(1)
{
if(s<k)
{
return (index[s]+num);
}
if(s%2==1){s=3*s+1;num++;}
else if(s%2==0){s=s/2;num++;}
}
}
void init()
{
int unsigned k1;
for(k1=1;k1<=1000000;k1++)
{
index[k1]=cacu(k1);
}
cout<<index[55]<<endl;
}
void mywork()
{
unsigned int k1,k2,sum=0;;
for(k1=i;k1<=j;k1++)
{
k2=index[k1];
if(sum<k2)sum=k2;
}
cout<<i<<" "<<j<<" "<<sum<<endl;
}
int main()
{
init();
while(cin>>i>>j)
{
if(i>j)
{
int temp=i;i=j;j=temp;
}
mywork();
}
return 0;
}
@end_of_source_code
I will never give up my dream

pahmed
New poster
Posts: 2
Joined: Wed Sep 25, 2002 3:35 am

3n+1 problem

every time i send this program, judge rely that " Wrong Answer". But it run correctly in my computer.can any one tell me what is the problem?

/*@judge_id: 22085KW 100 C */
#include<stdio.h>
int A(unsigned long int x);
void main()
{
int p,i,j,max=0,g,iReal,jReal;
scanf("%d %d\n",&i,&j);
iReal=i;jReal=j;
if (i>j)
{g=i;i=j;j=g;}
for (g=i;g<j+1;g++)
{
p=A(g);
if (p>max) max=p;
}

printf("%d %d %d\n",iReal,jReal,max);
}
int A(unsigned long int x)
{
int q=0;
an:
if (x==1){q++;return q;}
else if(x%2==1)
{x=3*x+1;q++;goto an;}
else
{x=x/2;q++;goto an;}

}
/*@end_of_source_code*/

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
Why do you have such line in your code ?

Code: Select all

``````cout<<index[55]<<endl;
``````
in init function ?? In my opinion this may cause error ....

Best regards
Dominik

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:
Well, your code seems to handle only one test-case ...

-turuthok-

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:
Don't forget when you already handle >1 test-case, print a new-line after the result of each test-case.

-turuthok-

fOOd_aCe
New poster
Posts: 1
Joined: Thu Oct 03, 2002 10:42 am

Can someone help me??

Well I don't know how to stop the cicle the gets the input. I've tried this but it doesn't seem to work.

[c]while(scanf(" %ld %ld", &i[n] &j[n]) == 2)
{
n++;
}
[/c]

I'd apreciate some help... Thanx[/c]

Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:
initialize n=0 at the start, and of course, put a comma between &i[n] and &j[n].

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
it should work ....
BTW I use scanf("%d %d" ..... without any leading spaces ....

your construction is very good and optimal for C - scanf returns number of successfully readed fields, also if you read 2 fields and got value of 2 as scanf'sreturn - it's correct.... in any other case you terminate loop statement ....

Best regards
Dominik

Rob Ferguson
New poster
Posts: 2
Joined: Thu Oct 03, 2002 9:26 pm
Location: Ms
Contact:

Problem 100 Help

Why does the judge tell me its the wrong answer. (Your program has not solved the problem. It ran during 4.820 seconds.) I get correct answers in Visual C++. Does anyone have any suggestions on how to correct my problem?

Thanks,
Rob
[cpp]
/* @JUDGE_ID: XXXXXX 100 C++ */
@BEGIN_OF_SOURCE_CODE
#include <iostream>
using namespace std;
int main() {
register long int n,i,j,k,l,c;
while(cin >> i >> j) {
if(i>j) {
k=i;i=j;j=k;
}
l=0;
k=i;
for (k;k<=j;k++) {
n=k;
c=0;
while (1) {
c++;
if (n==1) {
break;
}
if (n%2==1) {
n=(3*n)+1;
} else {
n/=2;
}
}
if (c>l) {
l=c;
}
}
cout << i << " " << j << " " << l << endl;
}
return 0;
}
@END_OF_SOURCE_CODE[/cpp]
Last edited by Rob Ferguson on Fri Oct 04, 2002 9:09 am, edited 3 times in total.

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:
Most of the times, the Online Judge is the one who's correct ...