您的位置:首页 > 其它

UVa 100 - The 3n + 1 problem

2016-04-30 20:45 323 查看
100 - The 3n + 1 problem

思路:算各个数的值,记录最大值

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>

using namespace std;

int cyc(int n) {
int cnt = 1;
while (n != 1) {
if (n % 2 != 0) n = 3 * n + 1;
else n = n / 2;
cnt++;
}
return cnt;
}

int main(void) {
int n, m, p, q;
int max;
while (cin >> n >> m) {
max = -1;
if (n > m) {
p = m;
q = n;
}
else {
p = n;
q = m;
}
for (int i = p; i <= q; i++) {
int temp = cyc(i);
if (max < temp) max = temp;
}
printf("%d %d %d\n", n, m, max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: