Solved
A really hard question .... NEED HELP ... !!!!
Posted on 2003-10-24
hi everyone ....
i got that problem that i coded but it doesn't give me the same that the sample output is giving ....
here is my question ......
==========================
Consider the following algorithm to generate a sequence of numbers.
Start with an integer n. If n is even, divide by 2. If n is odd, multiply
by 3 and add 1. Repeat this process with the new value of n, terminating
when n = 1.
For example, the following sequence of numbers will be generated for n = 22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
For an input n, the cycle-length of n is the number of numbers generated up
to and including the 1. In the example above, the cycle length of 22 is 16.
Given any two numbers i and j, you are to determine the maximum cycle length
over all numbers between i and j, including both endpoints.
Input
The input will consist of a series of pairs of integers i and j, one pair of
integers per line. All integers will be less than 1,000,000 and greater than 0.
Output
For each pair of input integers i and j, output i, j in the same order in which
they appeared in the input and then the maximum cycle length for integers between
and including i and j. These three numbers should be separated by one space,
with all three numbers on one line and with one line of output for each line of input.
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
=========================================
// my code and plz tell me what am i doing wrong
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
int test (int, int &);
void end (int);
void main ()
{
int i,j; // the cycle-length
int x=0, y=0; // the changes on "i" and "j"
int counter=0;
int count=0;
do
{
cin >> i ;
cin >> j;
}while (i < 0 || i > 1000000 || j < 0 || j > 1000000);
x=i;
y=j;
do
{
test(x, counter);
if (counter==1)
{
x=(x*3)+1;
count++;
}
else if (counter==2)
{
x=(x/2);
count++;
}
}while (x != 1);
do
{
test(y, counter);
if (counter==1)
{
y=(y*3)+1;
count++;
}
else if (counter==2)
{
y=(y/2);
count++;
}
}while ( y != 1);
end (count);
}
int test (int a, int &cntr)
{
if (a%2 != 0)
return cntr=1;
else if (a%2 == 0)
return cntr=2;
else
return cntr=0;
}
void end (int count)
{
cout << count << endl;
exit(0);
}