#include <iostream> using namespace std; int calc_cycle_length(int n) { int cycle_length = 1; while (n != 1) { if (n == 1) { return false; } else if (n % 2 != 0) { n = 3 * n + 1; } else { n = n / 2; } cycle_length++; } return cycle_length; } int main() { int firstNum, endNum; int temp = 0; int cycleLength = 0; int maxLength ; while (cin >> firstNum >> endNum && (firstNum != 0 || endNum != 0)) { cout<<firstNum<<" "<<endNum<<" "; maxLength = 0; if (firstNum > endNum) { temp = endNum; endNum = firstNum; firstNum = temp; } for (int i = firstNum; i < endNum; i++) { cycleLength = calc_cycle_length(i); if (cycleLength > maxLength) { maxLength = cycleLength; } } cout<<maxLength<<"\n"; } return 0; }拿到瘋狂程設run有一筆99999 99999這筆測資出不來,瘋狂程設這筆結果是要輸出227,但我只能輸出0..... 題目網址http://bit.ly/2eOC6q1
2017/3/9
100 - The 3n + 1 problem
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言