100 - The 3n + 1 problem
Moderator: Board moderators
Can't get
This time it accepted the code.
But said wrong answer.
I checked again.
My code is handling all the test cases
But said wrong answer.
I checked again.
My code is handling all the test cases
Code: Select all
#include<stdio.h>
int main()
{
unsigned long int a,b,c,d, loopvar, cycle_length=1, max_cycle_length=1, i;
scanf("%ld %ld",&a,&b);
printf("%ld %ld",a, b);
if(a>b)
{
a=a+b;
b=a-b;
a=a-b;
}
loopvar = (b-a) + 1;
c = a;
d = a;
for(i=1; i<=loopvar; i++)
{
while(c != 1)
{
if(c%2==0)
{
c = c/2;
}
else
{
c = 3*c + 1;
}
cycle_length++;
}
if(cycle_length >= max_cycle_length)
{
max_cycle_length = cycle_length;
}
d++;
c = d;
cycle_length=1;
}
printf(" %ld",max_cycle_length);
return 0;
}
Still doesnot work.
Code: Select all
#include<stdio.h>
int main()
{
unsigned long int a,b,c,d, loopvar, cycle_length=1, max_cycle_length=1, i;
while (scanf("%ld %ld",&a,&b)==2){
printf("%ld %ld",a, b);
if(a>b)
{
a=a+b;
b=a-b;
a=a-b;
}
loopvar = (b-a) + 1;
c = a;
d = a;
for(i=1; i<=loopvar; i++)
{
while(c != 1)
{
if(c%2==0)
{
c = c/2;
}
else
{
c = 3*c + 1;
}
cycle_length++;
}
if(cycle_length >= max_cycle_length)
{
max_cycle_length = cycle_length;
}
d++;
c = d;
cycle_length=1;
}
printf(" %ld\n",max_cycle_length);
}
return 0;
}
Heck
Did you at least test it?
Off course it DOESN'T work!
I'll explain the good way to test it :
open a new file in your notepad.
I'll take for granted it's name is 100.in.
copy all the sample input data, ALL the data in it.
your file should be:
100.in
"1 10
100 200
201 210
900 1000"
compile your code in 100.exe.
do following command
100.exe < 100.in
It should output exactly the sample output.
I'm quite sure your program doesn't handle the sample input correctly (can't compile on this computer) since the error is that cycle_length and max_cycle_length must be reinitialised each time you enter the while (scanf("%ld %ld",&a,&b)==2)
It will be Accepted after that...


Did you at least test it?
Off course it DOESN'T work!
I'll explain the good way to test it :
open a new file in your notepad.
I'll take for granted it's name is 100.in.
copy all the sample input data, ALL the data in it.
your file should be:
100.in
"1 10
100 200
201 210
900 1000"
compile your code in 100.exe.
do following command
100.exe < 100.in
It should output exactly the sample output.
I'm quite sure your program doesn't handle the sample input correctly (can't compile on this computer) since the error is that cycle_length and max_cycle_length must be reinitialised each time you enter the while (scanf("%ld %ld",&a,&b)==2)
It will be Accepted after that...


THANKS A LOT
THANKS A LOT.
It Worked...
It Worked...
-
- New poster
- Posts: 3
- Joined: Fri Sep 09, 2005 4:04 pm
problem# 100
Can anyone help me with problem#100 Vol.I
my code is (via C++)
*******************
using namespace std;
#include <iostream>
int main(){
long int i,j,k,cl,l;
long int max=0;
cin>>i>>j;
for( k=i;k<=j;k++){
cl=1;
l=k;
while(l!=1){
cl+=1;
if(l%2 !=0) l=l*3+1;
else l=l/2;
}
if(cl>max) max=cl;
}
cout<<i<<" "<<j<<" "<<max<<endl;
return 0;
}
******************
This code had beed compilied via Dev-C++ and Microsoft Visual C++ 6.0,however when I send this code to online-jude.uva.es it show me with wrong answer and minimum memory requrirement.
Thank in Advance
Masachutee
my code is (via C++)
*******************
using namespace std;
#include <iostream>
int main(){
long int i,j,k,cl,l;
long int max=0;
cin>>i>>j;
for( k=i;k<=j;k++){
cl=1;
l=k;
while(l!=1){
cl+=1;
if(l%2 !=0) l=l*3+1;
else l=l/2;
}
if(cl>max) max=cl;
}
cout<<i<<" "<<j<<" "<<max<<endl;
return 0;
}
******************
This code had beed compilied via Dev-C++ and Microsoft Visual C++ 6.0,however when I send this code to online-jude.uva.es it show me with wrong answer and minimum memory requrirement.
Thank in Advance
Masachutee
-
- New poster
- Posts: 3
- Joined: Fri Sep 09, 2005 4:04 pm
can anyone help me! with prob#100
Can anyone help me with problem#100 Vol.I
my code is (via C++)
*******************
using namespace std;
#include <iostream>
int main(){
long int i,j,k,cl,l;
long int max=0;
cin>>i>>j;
for( k=i;k<=j;k++){
cl=1;
l=k;
while(l!=1){
cl+=1;
if(l%2 !=0) l=l*3+1;
else l=l/2;
}
if(cl>max) max=cl;
}
cout<<i<<" "<<j<<" "<<max<<endl;
return 0;
}
******************
This code had beed compilied via Dev-C++ and Microsoft Visual C++ 6.0,however when I send this code to online-jude.uva.es it show me with wrong answer and minimum memory requrirement.
Thank in Advance
Masachutee
my code is (via C++)
*******************
using namespace std;
#include <iostream>
int main(){
long int i,j,k,cl,l;
long int max=0;
cin>>i>>j;
for( k=i;k<=j;k++){
cl=1;
l=k;
while(l!=1){
cl+=1;
if(l%2 !=0) l=l*3+1;
else l=l/2;
}
if(cl>max) max=cl;
}
cout<<i<<" "<<j<<" "<<max<<endl;
return 0;
}
******************
This code had beed compilied via Dev-C++ and Microsoft Visual C++ 6.0,however when I send this code to online-jude.uva.es it show me with wrong answer and minimum memory requrirement.
Thank in Advance
Masachutee
-
- New poster
- Posts: 3
- Joined: Fri Sep 09, 2005 4:04 pm
-
- New poster
- Posts: 2
- Joined: Tue Sep 13, 2005 8:53 pm
Prob #100 - can't find case to make it fail!
can't figure out where the problem can be in my code,
I have tested it with very different input types, and every time
I get the right results...
here is the code,
thanks
I have tested it with very different input types, and every time
I get the right results...
here is the code,
thanks
Code: Select all
#include <iostream>
using namespace std;
#define CACHE_LENGTH 100000
int steps[CACHE_LENGTH];
#ifdef ONLINE_JUDGE
#define LONG long long int
#else
#define LONG __int64
#endif
int GetSteps(LONG i)
{
if (i == 1)
return 1;
if ( i < CACHE_LENGTH )
{
if ( steps[i] == 0)
steps[i] = (i % 2) ? 2 + GetSteps( i + (i + 1) / 2 ) : 1 + GetSteps(i / 2);
return steps[i];
}
else
{
return (i % 2) ? 2 + GetSteps( i + (i + 1) / 2 ) : 1 + GetSteps(i / 2);
}
}
int main()
{
int a,b;
int max;
int delta;
while (1)
{
cin >> a >> b;
if ( cin.fail() )
break;
delta = a <= b ? 1 : -1;
max = 0;
for (int i = a; ((a != b && i != b) || (a == b && i == b) ); i+= delta)
{
int iSteps = GetSteps(i);
max = iSteps > max ? iSteps : max;
}
cout << a << " " << b << " " << max << endl;
}
return 0;
}