【题意】:获得最大循环长度。
【注意】:题目并不是一定按照第一二个数小于等于第二个数输入。如果不是,需要交换。。虽然这种数据感觉很无聊不过在题目中有提示的,The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
【AC代码】:
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <iomanip> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int getLength(int a) { int cnt = 1; while (a!=1) { cnt++; //even if (0==a%2) a = a/2; else a = 3*a+1; } return cnt; } int main(int argc, char** argv) { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int a = 0, b = 0; while (cin >> a >> b) { int m = 0, n = 0; m = a < b?a:b; n = a < b?b:a; int i = 0, max = -1; for (i = m; i <=n; i++) { if (max < getLength(i)) max = getLength(i); } cout << a << " " << b << " " << max << endl; } return 0; }
HDOJ 1032 The 3n + 1 problem(水)
原文地址:http://blog.csdn.net/weijj6608/article/details/43991555