|
//file Prog001v2.cpp //author AOU #include <iostream> using namespace std; /* input n1, n2 output the longest maxCycleLength */ /* Algorithm2: to display all the numbers and counts in a given range get n1 and n2 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 end for */ void main (void) { int n; int cycleLength; int n1, n2; 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"; } } /* 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 --------------- Press any key to continue */ |