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. PostgreSQL Replication之第十章 配置Slony(4)

    10.4 部署DDLs 对于生产性的应用程序来说,仅仅复制一个表明显是不够的.此外,通过没有办法保证数据从来不会发生改变.在某些时候,部署变化的数据结构(所谓的DDLs)是必要的. 现在的问题是,Sl ...

  2. HTML部分标签和代码

    1.1.1 一般标签.格式控制标签 所谓HTML就是 Hyper Text Markup Language(超无文本标记语言) <html>   开始标签 <head> 网页上 ...

  3. php面向对象(OOP)编程完全教程

    摘自:http://www.php-note.com/article/detail/41 面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行P ...

  4. CSS3 auto revolution practitioner!

    1.html <!DOCTYPE html> <html> <head> <title></title> <meta charset= ...

  5. HDU 3308 LCIS(线段树)

    Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (in ...

  6. JS获取项目根目录

    function getRootPath(){ //获取当前网址,如: http://localhost:8088/test/test.jsp var curPath=window.document. ...

  7. Oracle11.2新特性之listagg函数 (行列转换)

    SELECT regexp_substr('公司1,贵公司2', '[^,]+', 1, LEVEL, 'i') FROM dualCONNECT BY LEVEL <= length('公司1 ...

  8. 动态时间规整(DTW) 转载

    Dynamic Time Warping(DTW)诞生有一定的历史了(日本学者Itakura提出),它出现的目的也比较单纯,是一种衡量两个长度不同的时间序列的相似度的方法.应用也比较广,主要是在模板匹 ...

  9. 说说oracle分页的sql语句

    说说oracle分页的sql语句,分排序和不排序两种. 当结果集不需要进行排序时,每页显示条数为:rowPerPage,当前页数为:currentPage. 1. 相对来说,这种查询速度会快一些,因为 ...

  10. AndroidUI自动化测试工具-UIautomator

    转自:http://www.cnblogs.com/rexmzk/archive/2012/12/26/2834380.html 最近公司在开展Android的自动化测试,美国那边的开发人员利用And ...