Page 40 of 93
Posted: Sun Sep 04, 2005 3:29 pm
by chunyi81
hacker, your code is not handling more than one test case. Hope this helps.
Can't get
Posted: Sun Sep 04, 2005 5:09 pm
by hacker
This time it accepted the code.
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;
}
Posted: Mon Sep 05, 2005 4:22 am
by Cytoplasm
it is NOT multiple test case ready.
process inputs as a whole file and not line by line.
Posted: Tue Sep 06, 2005 12:24 am
by hacker
And how do i do that.
I really dun know how to do it.
Posted: Tue Sep 06, 2005 5:52 am
by Cytoplasm
instead of doing something like
scanf("%ld %ld",&a,&b);
code
do this:
while (scanf("%ld %ld",&a,&b)==2){
code;
}
Posted: Tue Sep 06, 2005 6:37 am
by hacker
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;
}
Posted: Tue Sep 06, 2005 8:24 am
by Cytoplasm
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...

THANKS A LOT
Posted: Tue Sep 06, 2005 9:53 am
by hacker
THANKS A LOT.
It Worked...
Posted: Fri Sep 09, 2005 1:03 am
by dantes
Just wondering in relation to java...apparently we can't use arrays to store existing calculations?
I am running a blank as to what else can be done to avoid recalculating certain numbers.
problem# 100
Posted: Fri Sep 09, 2005 4:12 pm
by masachutee
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
can anyone help me! with prob#100
Posted: Fri Sep 09, 2005 4:17 pm
by masachutee
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
Posted: Fri Sep 09, 2005 4:35 pm
by chunyi81
Masachutee, did you read angga888's post carefully?
Don't forget that the input can be more than one. You must read until EOF is encountered.
You have made the same mistake as Fadia.
Posted: Fri Sep 09, 2005 5:02 pm
by masachutee
Thank a lot , chunyi81
I have read Fadia's problems but did't carefully notice the answer of angga888.
reply on 100
Posted: Sat Sep 10, 2005 12:33 am
by roni
do u handle this case?
i can be smaller than j.
Prob #100 - can't find case to make it fail!
Posted: Mon Sep 19, 2005 8:02 pm
by patrickriva
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
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;
}