Problem A: The 3n + 1 problem

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 14  Solved: 6
[Submit][Status][Web Board]

Description

Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. For example, the following sequence of numbers will be generated for n = 22: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 It is conjectured (but not yet proven) that this algorithm will terminate at n = 1 for every integer n. Still, the conjecture holds for all integers up to at least 1, 000, 000. For an input n, the cycle-length of n is the number of numbers generated up to and including the 1. In the example above, the cycle length of 22 is 16. Given any two numbers i and j, you are to determine the maximum cycle length over all numbers between i and j, including both endpoints.

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 1,000,000 and greater than 0.

Output

For each pair of input integers i and j, output i, j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j. These three numbers should be separated by one space, with all three numbers on one line and with one line of output for each line of input.

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

HINT


  
  水题。计算从 i 到 j 的最大的周期数,一开始还担心数据规模太大而超时,后来看别人的AC代码发现就是道纯水题,测试数据都没有考虑极限情况,所以就安心提交上去通过了。

My code:

 #include <iostream>

 using namespace std;

 int main()
{
int f(int );
int i,j,ii,jj;
while(cin>>i>>j){
int l,max=;
if(i>j){
ii=j;jj=i;
}
else{
ii=i;jj=j;
}
for(int k=ii;k<=jj;k++){
l=f(k);
if(l>max)
max=l;
}
cout<<i<<' '<<j<<' '<<max<<endl;
}
return ;
} int f(int n)
{//计算 cycle-length ,返回 cycle-length 的值
int l=;
while(n!=){
if(n%==) //如果n是偶数
{
n/=;
}
else//n是奇数
{
n=n*+;
}
++l;
}
return l;
} /**************************************************************
Problem: 1098
User: freecode
Language: C++
Result: Accepted
Time:4 ms
Memory:1268 kb
****************************************************************/

Freecode : www.cnblogs.com/yym2013

烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)的更多相关文章

  1. Windows核心编程第一章.错误处理

    Windows核心编程第一章,错误处理. 一丶错误处理 1.核心编程学习总结 不管是做逆向,开始做开发.在Windows下.你都需要看一下核心编程这本书.这本书确实写得很好.所以自己在学习这本书的同时 ...

  2. .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...

  3. net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第一章 入门篇-开篇及总体规划

    .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划   原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来 ...

  4. Java 面向对象编程——第一章 初识Java

      第一章    初识Java 1.  什么是Java? Java是一种简单的.面向对象的.分布式的.解释的.安全的.可移植的.性能优异的多线程语言.它以其强安全性.平台无关性.硬件结构无关性.语言简 ...

  5. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  6. windows核心编程-第一章 对程序错误的处理

    第一章-对程序错误的处理 在开始介绍Microsoft Windows 的特性之前,必须首先了解 Wi n d o w s的各个函数是如何进行错误处理的. 当调用一个Wi n d o w s函数时,它 ...

  7. UNIX环境高级编程--第一章 UNIX基础知识

    第一章 UNIX基础知识 1.2 UNIX体系结构   从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境.我们将这种软件称为内核(kernel),因为 它相对较小,且 ...

  8. 读高性能JavaScript编程 第一章

    草草的看完第一章,虽然看的是译文也是感觉涨姿势了, 我来总结一下: 由于 大多数浏览器都是 single process 处理 ui updatas and js execute 于是产生问题: js ...

  9. Go Web 编程 第一章 Web相关概念

    第一章 Go与Web应用 Go学习群:415660935 1.1 Web应用 在计算机的世界里,应用(application)是一个与用户进行交互,并完成用户特定任务的软件程序.而Web应用则是部署在 ...

随机推荐

  1. Linux 下模拟Http 的get or post请求(curl和wget两种方法)

    一.get请求: 1.使用curl命令: curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地curl -i & ...

  2. 验证码点击刷新 this.src=this.src+'?'+Math.random()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. IE 兼容模式下不支持DIV CSS样式display:inline-block,解决

    样式改为: display: inline-block;*display: inline;zoom: 1; 就可以了

  4. 如何使用网盘托管git项目

    话说近年来git已经成为项目源代码管理的标准工具,有不少免费托管网站可供使用,详情参考这篇文章: http://www.cnblogs.com/zdz8207/archive/2012/05/20/2 ...

  5. TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!

    /** * * @author ocq */ class Parent implements Comparable { private int age = 0; public Parent(int a ...

  6. ThinkPHP访问不存在的模块跳到404页面

    在ACTION中新建一个文件EmptyAction.class.php,文件中的代码如下: <?php class EmptyAction extends Action{     functio ...

  7. Drainage Ditches(dinic)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59210   Accepted: 2273 ...

  8. php面试题之二——数据结构和算法(高级部分)

    二.数据结构和算法 1.使对象可以像数组一样进行foreach循环,要求属性必须是私有.(Iterator模式的PHP5实现,写一类实现Iterator接口)(腾讯) <?php class T ...

  9. [Effective JavaScript 笔记]第61条:不要阻塞I/O事件队列

    js程序是构建在事件之上的.输入可能来自不同的外部源.在一些语言中,我们习惯地编写代码来等待某个特定的输入. var text=downloadSync('http://example.com/fil ...

  10. L4 如何在XCode中下进行工作

    原地址:http://www.howzhi.com/course/286/lesson/2107 管理您的工作流程在Xcode 正如你在本教程中看到了  你的第一个iOS应用,你在Xcode工作区窗口 ...