|
//file Prog001v4.cpp //author AOU #include <iostream> using namespace std; /* input n1, n2 output the longest maxCycleLength */ /* Algorithm4: to display 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; cycleLength = 1; while (n > 1) { if (n%2 == 0) n = n/2; else n = 3*n + 1; cycleLength++; } if (cycleLength > maxCycleLength) maxCycleLength = cycleLength; } cout << n1 << ' ' << n2 << ' ' << maxCycleLength << endl; } /* 900 1000 900 1000 174 Press any key to continue */ |