//file Prog001v3.cpp
//author AOU
#include <iostream>
using namespace std;
/*
input n1, n2
output the longest maxCycleLength
*/
/*
Algorithm3: to display all the numbers and counts in a given range, and max count
get n1 and n2
maxCycleLength=0
for m=1 to n2
n = m
get a value for n
display n
cycleLength = 1
do while n > 1
if n is even then
n = n/2
else
n = 3*n + 1
end if
display n
cycleLength++
end while
display cycleLength
if cycleLength > maxCycleLength then
maxCycleLength = cycleLength
end for
output maxCycleLength
*/
void main (void)
{
int n;
int cycleLength;
int n1, n2;
int maxCycleLength=0;
cin >> n1 >> n2;
for (int m=n1; m<=n2; m++)
{
n = m;
//cin >> n;
cout << n << ' ';
cycleLength = 1;
while (n > 1)
{
if (n%2 == 0)
n = n/2;
else
n = 3*n + 1;
cout << n << ' ';
cycleLength++;
}
cout << endl;
cout << "cycleLength = " << cycleLength << endl;
cout << "---------------\n";
if (cycleLength > maxCycleLength)
maxCycleLength = cycleLength;
}
cout << n1 << ' ' << n2 << ' ' << maxCycleLength << endl;
}
/*
1 10
1
cycleLength = 1
---------------
2 1
cycleLength = 2
---------------
3 10 5 16 8 4 2 1
cycleLength = 8
---------------
4 2 1
cycleLength = 3
---------------
5 16 8 4 2 1
cycleLength = 6
---------------
6 3 10 5 16 8 4 2 1
cycleLength = 9
---------------
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
cycleLength = 17
---------------
8 4 2 1
cycleLength = 4
---------------
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
cycleLength = 20
---------------
10 5 16 8 4 2 1
cycleLength = 7
---------------
1 10 20
Press any key to continue
*/
|