一、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.  
  2. 1. input n
  3.  
  4. 2. print n
  5.  
  6. 3. if n = 1 then STOP
  7.  
  8. 4. if n is odd then n <-- 3n+1
  9.  
  10. 5. else n <-- n/2
  11.  
  12. 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).

二、题解



       这题真正的核心部分其实非常水,但是他的输入数据和输出要求有陷阱。首先,输入的时候要计较i和j的大小,然后不要忘了输出的时候也要换过来。这题除了暴力解决以外,还可以用到记忆化存储方法打表。这里有不水的解法http://blog.csdn.net/xieshimao/article/details/6774759。
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static int getCycles(int m){
  5. int sum=0;
  6. while(m!=1){
  7. if(m % 2==0){
  8. m=m / 2;
  9. sum++;
  10. }else{
  11. m=3*m+1;
  12. sum++;
  13. }
  14. }
  15. return sum+1;
  16. }
  17. public static void main(String[] args) {
  18. Scanner cin = new Scanner(System.in);
  19. int s,e,i;
  20. while(cin.hasNext()){
  21. s=cin.nextInt();
  22. e=cin.nextInt();
  23. boolean flag = false;
  24. if(s > e){
  25. int t=s;
  26. s=e;
  27. e=t;
  28. flag=true;
  29. }
  30. int max=Integer.MIN_VALUE;
  31. for(i=e;i>=s;i--){
  32. int sum=getCycles(i);
  33. if(sum>max)
  34. max=sum;
  35. }
  36. if(flag){
  37. System.out.println(e+" "+s+" "+max);
  38. }else
  39. System.out.println(s+" "+e+" "+max);
  40. }
  41. }
  42. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj1207 The 3n + 1 problem(水题(数据)+陷阱)的更多相关文章

  1. hdu-5867 Water problem(水题)

    题目链接: Water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  2. Codeforces - 1194B - Yet Another Crosses Problem - 水题

    https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...

  3. poj 1658 Eva's Problem(水题)

    一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...

  4. poj-1207 THE 3n+1 problem

    Description Problems in Computer Science are often classified as belonging to a certain class of pro ...

  5. HDU 5832 A water problem 水题

    A water problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  6. bestcoder 48# wyh2000 and a string problem (水题)

    wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K ...

  7. Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题

    C. NP-Hard Problem 题目连接: http://www.codeforces.com/contest/688/problem/C Description Recently, Pari ...

  8. Codeforces Round #603 (Div. 2) A. Sweet Problem 水题

    A. Sweet Problem the first pile contains only red candies and there are r candies in it, the second ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题

    A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...

随机推荐

  1. html/css背景图片自适应分辨率大小

    <style type='text/css'> .bgbox { position: absolute; left: 0; top: 0; width: 100%; overflow: h ...

  2. redis持久化AOF详细操作步骤

    1.切换到redis目录下面,创建文件 s3-redis.conf 2.编辑文件s3-redis.conf 3.终止当前redis服务端 4.登录redis客户端失败,说明服务端已停止 5.重启red ...

  3. css position: relative,absolute具体解释

    关于CSS中 position在布局中非常重要,查了非常多资料都说的非常难理解.以下说说个人的理解: 语法: position: relative | absolute relative: 对象遵循常 ...

  4. did not find a matching property (tomcat+Eclipse 报错)

    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclips ...

  5. Springmvc注解启用

      http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...

  6. linux查看当前文件夹下每个文件大小

    查看当前文件夹下每个文件大小 并会给出当前文件大小总和,后面加具体的文件名会显示具体的文件大小 ls -lht 把*换成具体的文件名会显示具体的文件大小 du -sh *

  7. MYSQL:基础——触发器

    MYSQL基础——触发器 引入触发器 什么是触发器 如果你想要某条语句(或某些语句)在事件发生时自动执行.比如: 每当订购一个产品时,都从库存数量中减去订购的数量:无论何时删除一行,都在某个存档表中保 ...

  8. iOS 基本数据类型 和 指针 特点

    基本数据类型 : 整型int, 字符型char , 浮点型 (float 和 double), 枚举型; -- 构造类型 : 数组类型, 结构体类型, 共用体类型; -- 指针类型 : 最终要的数据类 ...

  9. c的详细学习(8)指针学习(二)

    (1)指针与二维数组 一个数组的名字代表该数组的的首地址,是地址常量(作为形式参数的数组名除外),这一规定对二维数组或更高维数组同样适用. 在c语言中定义的任何一个二维数组实际上都可以看做是一个一维数 ...

  10. python 3 递归调用与二分法

    递归调用与二分法 1.递归调用 递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身. 示例: def age(n): if n == 1: return 18 # 结束条件 return a ...