这是一道很坑爹的题,一定注意输入的两个数的大小,并且不能简单的交换,因为在最后的输出的时候还需要将原来的数按照原来的顺序和大小,这就是为什么还得开辟两个值得原因

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<iostream>

#include <cmath>

using namespace std;

int main ()

{

 int m,n,w,j,i,max;

 while(cin>>m>>n)

 { 

  int x=m,y=n;

  if(m>n)

  { 

  int X=m;

           m=n;

     n=x;

  }

  max=-1;

  for(i=m;i<=n;i++)

  {

   j=i;

   w=0;

   while(j>1)

   {

    if(j%2!=0)

     j=3*j+1;

    else

     j=j/2;

    w++;

    //cout<<'*';

   }

     w++;

     if(max==-1)

      max=w;

     else

      if(max<w)

      max=w;

  }

 cout<<x<<' '<<y<<' '<<max<<endl;

 }

 return 0;

}

第二章 I - The 3n + 1 problem(2.4.2)的更多相关文章

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

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  2. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. UVA 100 - The 3n+1 problem (3n+1 问题)

    100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...

  4. classnull100 - The 3n + 1 problem

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正  The 3n + 1 problem  Background Problems in Computer Science are o ...

  5. CentOS 7.4 初次手记:第二章 CentOS安装步骤

    第二章 CentOS安装步骤... 18 第一节 下载... 18 第二节 分区参考... 18 第三节 安装... 19 I Step 1:引导... 19 II Step 2:配置... 20 I ...

  6. Gradle2.0用户指南翻译——第二章. 概述

    翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...

  7. 「学习记录」《数值分析》第二章计算实习题(Python语言)

    在假期利用Python完成了<数值分析>第二章的计算实习题,主要实现了牛顿插值法和三次样条插值,给出了自己的实现与调用Python包的实现--现在能搜到的基本上都是MATLAB版,或者是各 ...

  8. HttpClient学习研究---第二章:连接管理

    第二章.Connection management连接管理2.1. 2.1.Connection persistence连接持久性The process of establishing a conne ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

随机推荐

  1. collection的框架结构

    经常会看到程序中使用了记录集,常用的有Collection.HashMap.HashSet.ArrayList,因为分不清楚它们之间的关系,所以在使用时经常会混淆,以至于不知道从何下手.在这儿作了一个 ...

  2. JS代码实现网站设为首页加入收藏功能

    <script language="javascript"> //加入收藏 function AddFavorite(sURL, sTitle) { try { win ...

  3. Struts2 - Rest(2)

    (上篇:Struts2 - Rest(1)) 6) 加入user-index.jsp到/WEB-INF/content中: <%@ page language="java" ...

  4. 【shell】通配符

    ‘’与“” [root@andon ~]# name='$date' [root@andon ~]# echo $name $date [root@andon ~]# name=abc [root@a ...

  5. 剑指offer系列57---整数中1出现的次数

    [题目]求出1~n的整数中1出现的次数.(10进制) package com.exe11.offer; /** * [题目]求出1~n的整数中1出现的次数. * @author WGS * */ pu ...

  6. IntelliJ IDEA中怎么查看方法说明?

    View→Quick Documentation 查看当前配置的快捷键(例如Ctrl + Q) 在光标所在的方法上按下快捷键就可以看到方法的说明 下图为在View菜单中查看当前配置的快捷键截图: 下图 ...

  7. 报错:Caused by: java.io.FileNotFoundException: d:\youTemprepository\upload_77faffc1_1580a9240ca__8000_00000001.tmp (系统找不到指定的路径。)

    org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-dat ...

  8. C基础--函数参数副本

    转自:http://blog.csdn.net/chujiangke001/article/details/38553173 void GetMemory(char *p, int num) { p ...

  9. MyEclipse 设置注释

    MyEclipse设置注释的方法 windows-Preferences-(弹出窗口) 单击[Edit]后弹出编辑窗口,然后进行编辑

  10. 多线程同步 wait notify

    package test; public class Test implements Runnable{ public static int j =0; @Override public void r ...