|
//file Prog001v5.cpp //author AOU #include <iostream> using namespace std; /* input n1, n2 output the longest maxCycleLength */ /* Algorithm50: input n1 and n2 calculate maxCycleLength output n1, n2, maxCycleLength */ /* Algorithm51: input n1 and n2 //calculate maxCycleLength maxCycleLength = 0 for each value m in n1 and n2 do the following calculate the cycleLength for that value m update maxCycleLength if necessary end for output n1, n2, maxCycleLength */ /* Algorithm52: input n1 and n2 //calculate maxCycleLength maxCycleLength = 0 for each value m in n1 and n2 do the following //calculate the cycleLength for that value m n = m cycleLength = 1 while n>1 do the following if n is even then divide it by two else multiply it by three and add 1 cycleLength++ end while //update maxCycleLength if necessary if cycleLength > maxCycleLength then maxCycleLength = cycleLength end for output n1, n2, maxCycleLength */ void main (void) { int n; int cycleLength; int n1, n2; cin >> n1 >> n2; int maxCycleLength=0; 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; } /* 1 10 1 10 20 Press any key to continue */ |