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:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int f(int );
  8. int i,j,ii,jj;
  9. while(cin>>i>>j){
  10. int l,max=;
  11. if(i>j){
  12. ii=j;jj=i;
  13. }
  14. else{
  15. ii=i;jj=j;
  16. }
  17. for(int k=ii;k<=jj;k++){
  18. l=f(k);
  19. if(l>max)
  20. max=l;
  21. }
  22. cout<<i<<' '<<j<<' '<<max<<endl;
  23. }
  24. return ;
  25. }
  26.  
  27. int f(int n)
  28. {//计算 cycle-length ,返回 cycle-length 的值
  29. int l=;
  30. while(n!=){
  31. if(n%==) //如果n是偶数
  32. {
  33. n/=;
  34. }
  35. else//n是奇数
  36. {
  37. n=n*+;
  38. }
  39. ++l;
  40. }
  41. return l;
  42. }
  43.  
  44. /**************************************************************
  45. Problem: 1098
  46. User: freecode
  47. Language: C++
  48. Result: Accepted
  49. Time:4 ms
  50. Memory:1268 kb
  51. ****************************************************************/

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. thinkphp空控制器的处理

    <?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...

  2. The file couldn’t be opened because you don’t have permission to view it

    because you dont have permission to view it 解决办法 Project---Build Setting中 修改这一项,变成Default Compiler(A ...

  3. Linux里如何查找文件内容 (转)

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...

  4. jQuery特效

    基础特效 方法 描述 hide() 立即隐藏jQuery对象内的所有元素 hide(time).hide(time, easing) 在指定的时间内以动画方式隐藏jQuery对象内的所有元素,并可选一 ...

  5. C++小思

    Bjarne那稀疏的棕褐色头发, 有点红的眼睛, 这个可爱的好老头, 感觉他更应该是一个哲学家, 因为他用编程的语言C++ 揭示了我们这个纷繁复杂世界的本质: 对象. 对的, 世界是由对象组成的, 并 ...

  6. mysql 启动 导入sql文件

    mysql mysqld.exe mysqld.exe 是mysql的服务器进程,只有先启动这个进程才能连接服务器 dos下进入mysql文件目录下的bin目录,输入mysql -u root -p ...

  7. HDOJ 2389 Rain on your Parade

     HK.... Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K ...

  8. 淘宝(阿里百川)手机客户端开发日记第九篇 Looper详解

    public final class Looper: 官方的API: Class used to run a message loop for a thread. Threads by default ...

  9. 手动安装ubuntu视频播放器插件的方法

    新安装的ubuntu14.04在浏览器里面都不能看视频,提示缺少播放器插件,而且有一个安装的按钮,但是点击之后往往提示找不到,这就要手动安装了.第一步:首先运行一下更新命令吧sudo apt-get ...

  10. [Effective JavaScript 笔记]第41条:将原型视为实现细节

    对象原型链 一个对象给其使用者提供了轻量.简单.强大的操作集.使用者与一个对象最基本的交互是获取其属性值和调用其方法.这些操作不是特别在意属性存储在原型继承结构的哪个位置.随着时间推移,实现对象时可能 ...