100 - The 3n + 1 problem
Moderator: Board moderators
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Your program should be able to read & process multiple test cases. You may try using -
Code: Select all
while(scanf(" %ld %ld",&x,&y)==2)
{
//process
}
You should never take more than you give in the circle of life.
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Emilio wrote:The input haven't only a case.
You must check if x is greater than y or not.
By other hand, you must reuse another post that talk about the same problem, also there are a lot of post about this problem that you can have a look and I'm almost sure that they will resolve your doubts and mistakes.
Good luck!
got AC for prob no. 101 .. now i know for prob 100 end of input is not specified. how long to take the input?
and if x < y then what should i do ... ignore it and take the next input
or stop the prog execution...(no point in printing error msg)...
-
- New poster
- Posts: 37
- Joined: Fri Apr 30, 2004 6:52 pm
- Location: Portugal
changed my prog but still getting WA plz help..
whats the problem here now....plz help
Code: Select all
#include<stdio.h>
long max=0;
void process(long,long);
int main(void)
{
long x,y,a,b;
while(scanf("%ld%ld",&x,&y)!=EOF){
if(x>y){
a=x;
b=y;
process(b,a);
}
process(x,y);
printf("%ld %ld %ld\n",x,y,max);
max=0;
}
return 0;
}
void process(long x, long y)
{long i,count=0,n;
for(i=x;i<y;i++)
{
n=i;
while(1)
{
count++;
if(max<count)
max=count;
if(n==1)
{
count=0;
break;
}
if((n%2)==0)
n=n/2;
else
n=(3*n)+1;
}
}
}
whats the problem here now....plz help
-
- New poster
- Posts: 37
- Joined: Fri Apr 30, 2004 6:52 pm
- Location: Portugal
I think you're a little confused, let's see:
- You check if x > y, if so you use two variables to get the values of x and y and then process the input in the switched way...ok is correct but why the two extras variables? you can just do process(y,x) if x > y.
- Also if x > y you process with y first and then x (correct), after that you process again with x first and then y (wrong).
You're missing an else statement.
Be sure to test your program before submiting.
Code: Select all
long x,y,a,b;
while(scanf("%ld%ld",&x,&y)!=EOF){
if(x>y){
a=x;
b=y;
process(b,a);
}
process(x,y);
printf("%ld %ld %ld\n",x,y,max);
max=0;
}
- Also if x > y you process with y first and then x (correct), after that you process again with x first and then y (wrong).
You're missing an else statement.
Be sure to test your program before submiting.
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Still a lot of error left.
1. Swap (x,y) if x>y.
2. Be carefull about the output format. Your printf statement has extra space charecter which will immediately lead to wrong answer.
3. In the for loop within process, why should you write in this way?-
It must be
1. Swap (x,y) if x>y.
2. Be carefull about the output format. Your printf statement has extra space charecter which will immediately lead to wrong answer.
3. In the for loop within process, why should you write in this way?-
Code: Select all
for(i=x;i<y;i++)
Code: Select all
for(i=x;i<=y;i++)
You should never take more than you give in the circle of life.
Schutzstaffel wrote:
=viki= wrote
For her(I guess) code must swap the values if x > y.If x < y then you swap the value of y to x
=viki= wrote
For example in this manner:how long to take the input?
Code: Select all
while (scanf("%d%d",&x,&y)==2)
{
.....
}
-
- New poster
- Posts: 37
- Joined: Fri Apr 30, 2004 6:52 pm
- Location: Portugal
modified my code and got AC thanx for helping......
Code: Select all
oopss sorry my mistake... :oops:
Last edited by =viki= on Thu Jan 05, 2006 12:02 pm, edited 1 time in total.
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
To =viki=, You should read two numbers until EOF. So, use
And remember that x can be greater than y and both can be equal. So, change ..
Hope it helps.
Code: Select all
while(scanf("%ld%ld",&x,&y)==2)
{
//process
}
Code: Select all
void process(long x, long y)
{long i,count=0,n;
if(x>y)
//swap x,y
for(i=x;i<=y;i++) //i <= y
{
//Remaining code
}
}
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
HomePage
CE with problem 100 3N+1
Im getting discouraged here.
I know the algorithm. Ive compiled successfully on linux with gcc using the -ansi flag.
But yet Im getting the CE problem.
Here is the code:
/* 3N+1
*/
#include <stdio.h>
#include <stdlib.h>
int cycles(long);
int main (int argc, char **argv)
{
int result = 1;
long i=1;
int stepper = 1, max=1;
long firstnumber, secondnumber;
while (scanf("%ld %ld",&firstnumber, &secondnumber)!=EOF)
{
/*DEBUG
printf("\nfirstnumber = %ld",firstnumber);
printf("\nsecondnumber= %ld\n",secondnumber);
*/
if (firstnumber>secondnumber) stepper = -1;
for (i=firstnumber; i<=secondnumber; i=i+stepper)
{
result = cycles(i);
if (result > max) max=result;
result = 0;
}
printf("%ld %ld %ld\n",firstnumber, secondnumber, max);
firstnumber = 0;
secondnumber = 0;
max =1;
}
}
int cycles(long N)
{
int counter=1;
while (N!=1)
{
if (N%2==0) /*N is even*/
N=N/2;
else /*N is odd*/
N=N*3+1;
counter++;
}
return counter;
}
I know the algorithm. Ive compiled successfully on linux with gcc using the -ansi flag.
But yet Im getting the CE problem.
Here is the code:
/* 3N+1
*/
#include <stdio.h>
#include <stdlib.h>
int cycles(long);
int main (int argc, char **argv)
{
int result = 1;
long i=1;
int stepper = 1, max=1;
long firstnumber, secondnumber;
while (scanf("%ld %ld",&firstnumber, &secondnumber)!=EOF)
{
/*DEBUG
printf("\nfirstnumber = %ld",firstnumber);
printf("\nsecondnumber= %ld\n",secondnumber);
*/
if (firstnumber>secondnumber) stepper = -1;
for (i=firstnumber; i<=secondnumber; i=i+stepper)
{
result = cycles(i);
if (result > max) max=result;
result = 0;
}
printf("%ld %ld %ld\n",firstnumber, secondnumber, max);
firstnumber = 0;
secondnumber = 0;
max =1;
}
}
int cycles(long N)
{
int counter=1;
while (N!=1)
{
if (N%2==0) /*N is even*/
N=N/2;
else /*N is odd*/
N=N*3+1;
counter++;
}
return counter;
}