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. User表格式

    "_id":基本是700多 "name":"xx01" "pwd":"123"

  2. hdu 1008 Elevator

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The hig ...

  3. Clion = C/C++ 和 Python 共享的 IDE

    Clion + Tdmgcc + Winpython(Python)

  4. PHP iconv函数字符串转码导致截断问题

    1.iconv函数原型 string iconv ( string $in_charset , string $out_charset , string $str ) in_charset:输入的字符 ...

  5. SQL Server 2005导入Excel表问题

    EXCEL导入到SQL Server经常出现“文本被截断,或者一个或多个字符在目标代码页中没有匹配项” 原因: SQL Server的导入导出为了确定数据表的字段类型,取excel文件的前8行来判别. ...

  6. How can I style a JavaFX SplitMenuButton in CSS

    0 down vote favorite I try to style a SplitMenuButton in JavaFX. I've got a menuButton and a SplitMe ...

  7. 使用XtraGrid自定义列计算1 z

    Devexpress控件集提供的DataGrid控件,在功能和界面样式上都完爆WinForm的DataGridView控件,以前需要在 DataGridView控件上进行某列的统计,需要在GridVi ...

  8. javascript中parentNode,childNodes,children的应用详解

    本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助   "parentNode&qu ...

  9. jsp&servlet学习笔记

    1.路径引用问题 一个css.jsp.html.或者javascript文件从从一个工程复制到另一工程,如果引用的时候使用的时相对路径,看似没有错误,但是却一直引用不进来,这时候要使用绝对路径,这样才 ...

  10. Linux 查看系统用户的登录日志

    查看用户登录系统的日志   有两类日志记录用户登录的行为,一是记录登录者的数据,一个是记录用户的登录时间   一,记录用户登录数据         /var/log/wtmp日志文件记录用户登录的数据 ...