100 - The 3n + 1 problem
Moderator: Board moderators
-
- 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. -_-;
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. -_-;
-
- 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]
Try a little code for each one, and you'll get it..[/cpp]
-
- 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
/* @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
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*/
/*@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*/
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Why do you have such line in your code ?
in init function ?? In my opinion this may cause error ....
Best regards
Dominik
Code: Select all
cout<<index[55]<<endl;
Best regards
Dominik
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]
[c]while(scanf(" %ld %ld", &i[n] &j[n]) == 2)
{
n++;
}
[/c]
I'd apreciate some help... Thanx[/c]
-
- 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
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
-
- 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]
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.