100 - The 3n + 1 problem
Moderator: Board moderators
-
- New poster
- Posts: 10
- Joined: Mon May 07, 2007 4:45 am
Hey to all of you programming pros out there can someone plz tell me what is wrong with my program I always get Compiler Error for some reason and I have no idea what is wrong with this because it works fine on my computer GRR
#include<iostream>
#define MAXSIZE 100000
using namespace std;
int find_length(int num)
{
if(num==1) return 1;
int n=num;
if(n%2==0) n/=2;
else n=n*3+1;
return find_length(n)+1;
}
int main()
{
long min, max;
while(cin>>min>>max)
{
cout<<min<<" "<<max<<" ";
if(min>max)
{
long a=min; min=max; max=a;
}
long max_length=0;
for(long i=min; i<=max; i++)
{
long n=find_length(i);
if(n>max_length)
max_length=n;
}
cout<<max_length<<endl;
}
return 0;
}
Thanks for helping
btw sometimes it says "received". What does that mean??
#include<iostream>
#define MAXSIZE 100000
using namespace std;
int find_length(int num)
{
if(num==1) return 1;
int n=num;
if(n%2==0) n/=2;
else n=n*3+1;
return find_length(n)+1;
}
int main()
{
long min, max;
while(cin>>min>>max)
{
cout<<min<<" "<<max<<" ";
if(min>max)
{
long a=min; min=max; max=a;
}
long max_length=0;
for(long i=min; i<=max; i++)
{
long n=find_length(i);
if(n>max_length)
max_length=n;
}
cout<<max_length<<endl;
}
return 0;
}
Thanks for helping
btw sometimes it says "received". What does that mean??
-
- New poster
- Posts: 10
- Joined: Mon May 07, 2007 4:45 am
100 - Time Exceeded (?)
Hi. I've received a time exceeded (10.025 seconds) when in other computers, the program takes 0 seconds. Could it be the entrance mode? Here is the code. Thanks in advance:
#include "stdio.h"
void main(){
long n,i,x,n0,n1,mc,c=1;
while (scanf("%ld %ld",&n0,&n1)) {
printf ("%ld %ld ",n0,n1);
mc=1;
if (n0>n1) {x=n0;n0=n1;n1=x;}
for (i=n0;i<=n1;i++) {
c=1;
n=i;
while(n>1){
n=(n%2)?(3*n+1):(n/2);
c++;
}
if (c>mc) mc=c;
}
printf ("%ld\n",mc);
}
}
#include "stdio.h"
void main(){
long n,i,x,n0,n1,mc,c=1;
while (scanf("%ld %ld",&n0,&n1)) {
printf ("%ld %ld ",n0,n1);
mc=1;
if (n0>n1) {x=n0;n0=n1;n1=x;}
for (i=n0;i<=n1;i++) {
c=1;
n=i;
while(n>1){
n=(n%2)?(3*n+1):(n/2);
c++;
}
if (c>mc) mc=c;
}
printf ("%ld\n",mc);
}
}
100 - 3n+1 - Memorisation
I have solved the 3n+1 problem by use of the memorisation method. In my computer, it solves the input "1 999999" 60 times faster than brute force. But when I submit it, besides getting AC, it takes over a second to get it correct, considering that the brute force took 3 seconds. Furthermore, I expected the 4000000 elements memorisation table to be built once only, taking no more than 0.1 seconds, but I am strongly suspecting my program is being loaded up more than once.
Finally, I want to ask how many times does the judge call my main function, i.e. how many times does it start up my program. Until today I thought it was once only but I am not sure it is.
I would post the code, but it is AC.
Thanks in advance.
Finally, I want to ask how many times does the judge call my main function, i.e. how many times does it start up my program. Until today I thought it was once only but I am not sure it is.
I would post the code, but it is AC.
Thanks in advance.
Search the board first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 20
- Joined: Mon May 28, 2007 4:36 pm
- Location: India
suggestion to Notta Benna
To Notta Benna
you are taking only one set of input and printing out the answer
you have to take the input sets in a loop till the end of file.
you can read in one set of input , print out the output then again read in next set....
go on till there is no more input
you are taking only one set of input and printing out the answer
you have to take the input sets in a loop till the end of file.
you can read in one set of input , print out the output then again read in next set....
go on till there is no more input
-
- New poster
- Posts: 1
- Joined: Sun May 20, 2007 7:18 am
- Location: kharagpur
MEMORIZATION
I am new to this world of programming . Can someone tell me what memorization is?? I got a TLE error when the judge executed my code
>>diptesh_chatterjee
I didn't do any kind of memorization & i got AC in 3.393 sec.
Here u can store the chain length. u must not calculate the chain length for a number twice.
here is an example
let i = 3 & j = 6
then first step u calculate the chain length for 3 is 8.
chain length for 4 is 3.
chain length for 5 ->16 ->8 ->4(add cycle length of 4)need no further calculation.
chain length for 6->3(add cycle length of 3) only one operation need.
Hope it helps.
I didn't do any kind of memorization & i got AC in 3.393 sec.
Here u can store the chain length. u must not calculate the chain length for a number twice.
here is an example
let i = 3 & j = 6
then first step u calculate the chain length for 3 is 8.
chain length for 4 is 3.
chain length for 5 ->16 ->8 ->4(add cycle length of 4)need no further calculation.
chain length for 6->3(add cycle length of 3) only one operation need.
Hope it helps.
How to terminate the input
I'v been tryin out problem number 100, n the problem i'v been havin is in terminating the input.....
problems statement says tht each test case would be in a new line...like
10 100
20 200
nw how to terminate tht....
i thought this would work
while ( !feof ( stdin ) )
{
cin >> a; cin >> b;
//process
}
but this code is givin me run-time exceed error,
so i tried out the desi version
while ( scanf ( %d %d ), &a, &b ) != EOF )
{
//process
}
n this method works..
so nw wht is the correct way of terminating the input, what if i want the design to be like
while ( true )
{
cin >> a; cin >> b;
//process
if ( /*chk_condition */ )
break;
}
what should be the chk_condition over here
thx in anticipation..
problems statement says tht each test case would be in a new line...like
10 100
20 200
nw how to terminate tht....
i thought this would work
while ( !feof ( stdin ) )
{
cin >> a; cin >> b;
//process
}
but this code is givin me run-time exceed error,
so i tried out the desi version
while ( scanf ( %d %d ), &a, &b ) != EOF )
{
//process
}
n this method works..
so nw wht is the correct way of terminating the input, what if i want the design to be like
while ( true )
{
cin >> a; cin >> b;
//process
if ( /*chk_condition */ )
break;
}
what should be the chk_condition over here
thx in anticipation..
