题意:在[l, r]中找到3n+1猜想最大步数的数字
解题思路: 在[l, r]中枚举每个数字,每次枚举完都更新最大值
注意事项: 给出的两个数字大的在前小的在后时,交换一下便于处理
参考代码:
#include <bits/stdc++.h> using namespace std; int main() { int l, r; while (scanf("%d%d", &l, &r) != EOF) { printf("%d %d ", l, r); //先输出 if (l > r) swap(l, r); //需要交换就交换一下,确保小数在前 int mlen = 0, tlen = 0; //最大步数,当前步数 for (int i = l, temp = l; i <= r; i++, temp = i) { //枚举区间中的每个数字 tlen = 1; while (temp != 1) { //3n+1猜想 if (temp % 2) temp = temp * 3 + 1; else temp /= 2; tlen++; } mlen = max(mlen, tlen); //保留最大值 } printf("%d\n", mlen); } return 0; }
0.0分
3 人评分