Code: Select all
#include <iostream>
using namespace std;
#define MAX 1000000 // max int for input
// cycle_length_array[i] store the value of int i, init value is 0
int cycle_length_array[MAX];
int generate_cycle_length(int n);
int get_max_cycle_length(int start, int end);
int main()
{
int start = 0;
int end = 0;
int max = 0;
while (cin>>start>>end)
{
if (start > end)
{
max = get_max_cycle_length(end, start);
}
else
{
max = get_max_cycle_length(start, end);
}
cout << start << " "<< end << " "<< max<<endl;
}
return 1;
}
int generate_cycle_length(int n)
{
int count = 1;
while (n != 1)
{
if (n % 2)
{
n = 3 * n + 1;
}
else
{
n /= 2;
}
count += 1;
}
return count;
}
int get_max_cycle_length(int start, int end)
{
int max = 0;
for (int i = start; i <= end; ++i)
{
if (cycle_length_array[i] == 0)
{
cycle_length_array[i] = generate_cycle_length(i);
}
if (cycle_length_array[i] > max)
{
max = cycle_length_array[i];
}
}
return max;
}
have any one get runtime : 0.008