¶
2005-12-30 14:39:20 Accepted 3.256 436 56031 C++ 100 - The 3n + 1 problem
감 ¶
게 Accepted 감격 겨 .
. 꼼꼼 각 .
. 꼼꼼 각 .
¶
1. 2개 . ( 경 )
2. 꼈. (& ), 기2(right shift 1) - .
3. 고 . - .
4. 기 - .
2. 꼈. (& ), 기2(right shift 1) - .
3. 고 . - .
4. 기 - .
¶
~cpp // The 3n + 1 problem // UVa ID : 100 #include <iostream> using namespace std; int cycle_length(int input); int main() { int input1, input2; while (cin >> input1 >> input2) { int i; int max_count = -1; int temp = 0; // !! cout << input1 << " " << input2 << " "; // 경 (for ) if (input1 > input2) { int swap = input1; input1 = input2; input2 = swap; } for (i = input1; i <= input2; i++) { temp = cycle_length(i); // cycle legnth 기 if (temp > max_count) max_count = temp; // max_count } cout << max_count << endl; } return 0; } // cycle length 구기 int cycle_length(int input) { int argument = input; // int count = 0; // while (true) { // if (argument == 1) { count++; break; } // LSB 0 , 1 . if ((argument & 1) == 0) { // 기 2 right shift 과 . argument >>= 1; count++; } else { // . argument = 3 * argument + 1; argument >>= 1; count += 2; } } return count; }