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建立用户 详细

    .你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...

  2. xss跨站实例总结

    跨站脚本攻击(Cross-site scripting,通常简称为XSS)发生在客户端,可被用于进行窃取隐私.钓鱼欺骗.偷取密码.传播恶意代码等攻击行为. 恶意的攻击者将对客户端有危害的代码放到服务器 ...

  3. WCF报 当前已禁用此服务的元数据发布的错误

    这是 Windows© Communication Foundation 服务. 当前已禁用此服务的元数据发布. 如果具有该服务的访问权限,则可以通过完成下列步骤来修改 Web 或应用程序配置文件以便 ...

  4. C++中的异常处理(二)

    C++中的异常处理(二) 标签: c++C++异常处理 2012-11-24 20:56 1713人阅读 评论(2) 收藏 举报  分类: C++编程语言(24)  版权声明:本文为博主原创文章,未经 ...

  5. Flume-NG内置计数器(监控)源码级分析

    Flume的内置监控怎么整?这个问题有很多人问.目前了解到的信息是可以使用Cloudera Manager.Ganglia有图形的监控工具,以及从浏览器获取json串,或者自定义向其他监控系统汇报信息 ...

  6. 微信公众号"赞赏"功能来了 觉得不错就给作者打个赏吧

    微信很早以前就开始测试“赞赏”功能了,只是官方还没出公告,近日腾讯科技就发了一篇题为 试试给微信公众号“赞赏” 的文章,算是一个回应吧.微信打赏功能势在遏制公众账号抄袭,鼓励用户创造优质内容,内容付费 ...

  7. sql大全

    推荐一. 简单查询   简单的Transact-SQL查询只包括选择列表.FROM子句和Where子句.它们分别说明所查询列.查询的表或视图.以及搜索条件等. 例如,下面的语句查询testtable表 ...

  8. ubuntu同步系统时间命令

      *设置时区的命令 sudo tzselect 然后一步步选择就行 *同步时间的命令 sudo ntpdate ntp.ubuntu.com

  9. :( Call to a member function Table() on a non-object 错误位置

    :( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form

  10. Coursera台大机器学习技法课程笔记02-Dual Support Vector Machine

    这节课讲的是SVM的对偶问题,比较精彩的部分:为何要使用拉格朗日乘子以及如何进行对偶变换. 参考:http://www.cnblogs.com/bourneli/p/4199990.html http ...