The 3n + 1 problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53927   Accepted: 17142

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
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174
#include <cstdio>
#include <string.h>
#include <cmath>
#include <iostream>
#define WW freopen("output.txt","w",stdout)
using namespace std;
const int Max=11000;
int Arr[Max];
int main()
{
memset(Arr,0,sizeof(Arr));
Arr[1]=1;
Arr[2]=2;
for(int i=3; i<Max; i++)
{
int ans=i;
int sum=1;
while(ans!=1)
{
if(ans<i)
{
break;
}
if(ans%2)
{
ans=ans*3+1;
}
else
{
ans/=2;
}
++sum;
}
Arr[i]=Arr[ans]+sum-1;
}
int Star,End;
int vis;
while(~scanf("%d %d",&Star,&End))
{
vis=0;
if(Star>End)//可能输入的开始比结束大,所以要交换
{
swap(Star,End);
vis=1;
}
int MAX=0;
int flag=0;
for(int i=Star; i<=End; i++)
{
if(MAX<Arr[i])
{
MAX=Arr[i];
flag=i;
}
}
if(!vis)//输出时要按照输入的顺序
printf("%d %d %d\n",Star,End,Arr[flag]);
else
{
printf("%d %d %d\n",End,Star,Arr[flag]);
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏的更多相关文章

  1. Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 26678   Accepted: ...

  2. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  3. strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏

    (一) strace 命令    用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...

  4. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  5. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  6. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  7. max_flow(Ford-Fulkerson) 分类: ACM TYPE 2014-09-02 01:50 110人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> #include<queue> usi ...

  8. JAVA 关于Icon,Image,ImageIcon的简单的对比参考 分类: Java Game 2014-08-14 17:08 210人阅读 评论(0) 收藏

    转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...

  9. c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏

    使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...

随机推荐

  1. MAX(A,B)

    MAX(A,B)  可以把x,或者y的变量扔进去比较,会自动放出比较结果,这样就避免的三目运算.

  2. HTML--表单,图片热点,网页划区和拼接

    一.图片热点 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: <img src="/ usemap="longzhu"> ...

  3. [转]数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

    一.MySQL   MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...

  4. java的I/O操作:文件的路径

    package solutions; import java.io.*; /** * Created by Administrator on 2016/3/14. */ public class Re ...

  5. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  6. TP隐藏入口

    我们知道,在thinkphp的案例中有一个.htaccess文件,里面配置了URL的一些重写规则,如: <IfModule mod_rewrite.c>  RewriteEngine on ...

  7. angular 解析html

    方法1:写filter <div ng-bind-html="showContent | html" class="detail-content"> ...

  8. java 虚拟机工具

    jps 命令 可以查询开启了rmi服务的远程虚拟机进程状态. -v jvm参数. jstat -gcutil命令 [cangyue@/System/Library/Frameworks/JavaVM. ...

  9. yii2中表单的字段标签名称

    1.以登陆页面为例,默认是英文的,在loginForm.php中添加attributeLabels,可以变成中文 具体代码如下: public function attributeLabels(){  ...

  10. Sql server analysis service 通过IIS连接时的最大连接数问题

    做过SSAS项目的大部分人都应该知道SSAS是可以通过在IIS上建立代理站点的方式来建立远程连接的,这样可以绕过连接到SSAS时需要在同一个域环境下通过域用户来验证的问题,这样即使连接到SSAS的客户 ...