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;
}
思路:算各个数的值,记录最大值
#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;
}
相关文章推荐
- 1047. 编程团体赛(20)
- awk工具简介
- hdu 3065 AC自动机(各子串出现的次数)
- myBatis查询
- C#OOP之十三 组件、程序集与反射
- C#OOP之十三 组件、程序集与反射
- 1046. 划拳(15)
- 11个最常用的AJAX框架
- vc6.0 图片显示(个人笔记)
- C#OOP之十三 组件、程序集与反射
- 用C++写Stash
- 计算n!的位数
- java 插入表记录后得到自增的id (转) (附3种方法代码)
- JDBC笔记-PreparedStatement
- Java网络编程的框架
- ZOJ-3933 Team Formation (二分图最佳完美匹配)
- 搭建Maven(三)使用Nexus搭建Maven私服
- [可并堆] BZOJ 2333 [SCOI2011]棘手的操作
- frame 与 bounds 的区别与关系
- Longest Increasing Subsequence Review