100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

hacker, your code is not handling more than one test case. Hope this helps.
hacker
New poster
Posts: 5
Joined: Fri Jun 10, 2005 11:45 pm

Can't get

Post 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;
}
Cytoplasm
New poster
Posts: 13
Joined: Tue Jun 14, 2005 6:33 pm
Location: Paris, France

Post by Cytoplasm »

it is NOT multiple test case ready.

process inputs as a whole file and not line by line.
hacker
New poster
Posts: 5
Joined: Fri Jun 10, 2005 11:45 pm

Post by hacker »

And how do i do that.

I really dun know how to do it.
Cytoplasm
New poster
Posts: 13
Joined: Tue Jun 14, 2005 6:33 pm
Location: Paris, France

Post by Cytoplasm »

instead of doing something like

scanf("%ld %ld",&a,&b);

code

do this:

while (scanf("%ld %ld",&a,&b)==2){
code;
}
hacker
New poster
Posts: 5
Joined: Fri Jun 10, 2005 11:45 pm

Post 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;
}
Cytoplasm
New poster
Posts: 13
Joined: Tue Jun 14, 2005 6:33 pm
Location: Paris, France

Post by Cytoplasm »

Heck :cry:

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...

:cry: :cry:
hacker
New poster
Posts: 5
Joined: Fri Jun 10, 2005 11:45 pm

THANKS A LOT

Post by hacker »

THANKS A LOT.

It Worked...
dantes
New poster
Posts: 1
Joined: Fri Sep 09, 2005 1:01 am

Post 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.
masachutee
New poster
Posts: 3
Joined: Fri Sep 09, 2005 4:04 pm

problem# 100

Post 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
masachutee
New poster
Posts: 3
Joined: Fri Sep 09, 2005 4:04 pm

can anyone help me! with prob#100

Post 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
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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.
masachutee
New poster
Posts: 3
Joined: Fri Sep 09, 2005 4:04 pm

Post by masachutee »

Thank a lot , chunyi81
I have read Fadia's problems but did't carefully notice the answer of angga888.
roni
New poster
Posts: 11
Joined: Tue Aug 09, 2005 11:57 am
Location: SUST, BANGLADESH
Contact:

reply on 100

Post by roni »

do u handle this case?
i can be smaller than j.
roni(SUST)
patrickriva
New poster
Posts: 2
Joined: Tue Sep 13, 2005 8:53 pm

Prob #100 - can't find case to make it fail!

Post 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; 
} 
Post Reply

Return to “Volume 1 (100-199)”