poj1207 3n+1 problem
The 3n + 1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
Consider the following algorithm: 1. input n 2. print n 3. if n = 1 then STOP 4. if n is odd then n <-- 3n+1 5. else n <-- n/2 6. GOTO 2 Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.) Given an input n, it is possible to determine the number of numbers printed before the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16. For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j. Input The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 10,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j. Output For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. 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).
Sample Input 1 10 Sample Output 1 10 20 Source |
[Submit] [Go Back] [Status] [Discuss]
解题思路:题不难,但是改了好多次才ac的。
1.要注意输入的两个数不一定是i<j的,需要排序。
2.最后输出的i和j要跟刚开始输入的顺序一致,不能输出排序之后的。
#include <iostream>
#include <cstdio> using namespace std; int cal(int t){
int countt=;
while(t!=){
countt++;
if(t&==){
t=*t+;
}else{
t/=;
}
}
return countt;
} int main()
{
int a,b;
int maxx=;
int noww=;
while(~scanf("%d %d",&a,&b)){
cout<<a<<' '<<b<<' ';
if(a>b){
a^=b;
b^=a;
a^=b;
}
maxx=;
for(int i=a;i<=b;i++){
noww=cal(i);
maxx=noww>maxx? noww:maxx;
}
cout<<maxx<<endl;
}
return ;
}
poj1207 3n+1 problem的更多相关文章
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)
Problem A: The 3n + 1 problem Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 6[Submit][St ...
- The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53927 Accepted: 17 ...
- uva----(100)The 3n + 1 problem
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
// The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...
- 100-The 3n + 1 problem
本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...
- PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
- classnull100 - The 3n + 1 problem
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 The 3n + 1 problem Background Problems in Computer Science are o ...
随机推荐
- 用 Mathematica 获取图片的 RGB 三基色
ColorConvert[*, "RGB"] // InputForm 其中 * 表示你把你的图片拖入 Mathematica 中.
- [物理学与PDEs]第1章习题11 各向同性导体中电荷分布的指数衰减
在各向同性的导体中, Ohm 定律具有如下形式: $$\bex {\bf j}=\sigma {\bf E}, \eex$$ 其中 $\sigma$ 称为电导率. 试证在真空中导体的连续性方程为 $$ ...
- react-router v4中 HashRouter 和 BrowserRouter的使用
遇到的问题 项目中控制路由跳转使用的是BrowserRouter,代码如下: ReactDOM.render(( <BrowserRouter> <div className=&qu ...
- iTOP-4418开发板Qt系统下运行摄像头测试程序
编译环境:Ubuntu 12.04 交叉编译工具链:gcc 4.4.1 一.添加编译器的环境变量 打开~/.bashrc文件,修改环境变量,如下图: 修改完后,更新环境变量,使用命令”source ...
- Linux文件权限命令及配置
http://www.cnblogs.com/CgenJ/archive/2011/07/28/2119454.html
- How to avoid the 0-byte file on Webclient download error
_client.DownloadDataAsync(new Uri(url)); _client.DownloadDataCompleted += (sender, e) => { try { ...
- 肺结节CT影像特征提取(四)——肺结节CT影像特征提取MATLAB代码实现
之前的文章讲述了肺结节CT影像数据特征提取算法及基于MATLAB GUI设计的肺结节CT影像特征提取系统.本文将讲述几个主要部分的代码实现,分别是预处理.灰度特征提取.纹理特征提取.形态特征提取数据. ...
- Lua中的表达式
[算术操作符] Lua支持常规的算术操作符有:”+”(加法),”-“(减法),”*”(乘法),”/”(除法),”^”(指数),”%”(取模),一元的”-“(负号).所有的这些操作符都用于实数.例如:x ...
- 【easy】118.119.杨辉三角
这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...
- CentOS 7 中 Systemd详解
一.systemd的由来 Linux一直以来采用init进程但是init有两个缺点: 1.启动时间长.Init进程是串行启动,只有前一个进程启动完,才会启动下一个进程.(这也是CentOS5的主要特征 ...