一、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).

二、题解



       这题真正的核心部分其实非常水,但是他的输入数据和输出要求有陷阱。首先,输入的时候要计较i和j的大小,然后不要忘了输出的时候也要换过来。这题除了暴力解决以外,还可以用到记忆化存储方法打表。这里有不水的解法http://blog.csdn.net/xieshimao/article/details/6774759。
import java.util.Scanner; 

public class Main {
public static int getCycles(int m){
int sum=0;
while(m!=1){
if(m % 2==0){
m=m / 2;
sum++;
}else{
m=3*m+1;
sum++;
}
}
return sum+1;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int s,e,i;
while(cin.hasNext()){
s=cin.nextInt();
e=cin.nextInt();
boolean flag = false;
if(s > e){
int t=s;
s=e;
e=t;
flag=true;
}
int max=Integer.MIN_VALUE;
for(i=e;i>=s;i--){
int sum=getCycles(i);
if(sum>max)
max=sum;
}
if(flag){
System.out.println(e+" "+s+" "+max);
}else
System.out.println(s+" "+e+" "+max);
}
}
}

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

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. spring web app的结构

    1 入口是web.xml tomcat加载war的时候会去读该入库文件. 2 web.xml中spring mvc的配置 定义servlet到servlet-mapping之间的映射,org.spri ...

  2. iOS 运行时详解

    注:本篇文章转自:http://www.jianshu.com/p/adf0d566c887 一.运行时简介 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行 ...

  3. Netty Redis 亿级流量 高并发 实战 (长文 修正版)

    目录 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -30[ 博客园 总入口 ] 写在前面 1.1. 快速的能力提升,巨大的应用价值 1.1.1. 飞速提升能力,并且满足实际开发要求 1 ...

  4. js城市联动选择器

    <html> <head> <META charset="utf8"> <script type="text/javascrip ...

  5. Django 之ModelForm

    1.Form表单的回顾 Model - 数据库操作 - 验证 class A(MOdel): user = email = pwd = Form - class LoginForm(Form): em ...

  6. unix网络编程笔记(二)

    第四章笔记 1. 基本Tcpclient/server程序的套接字函数 2. socket函数: int socket(int family,int type,int protocol); (1)so ...

  7. Hadoop初体验

    1.首先准备环境 系统:Linux(centOS) jdk:1.7 这里jdk要安装配置完成,具体步骤参考:Linux环境下安装JDK 注意:本次没有配置免密登录,所以在启动和停止的时候回让你输入多次 ...

  8. Docker学习总结之docker创建私有仓库(private Repositories)

    Docker 创建 Private Repositories 前言 基于GFW的缘故,国内大陆基本无法pull国外的镜像,更别说官方的index了.如果images无法pull下来,那么docker就 ...

  9. python 发送邮件的两种方式【终极篇】

    一,利用python自带的库 smtplib简单高效 from email.mime.multipart import MIMEMultipart from email.mime.text impor ...

  10. sqlalchemy——多表操作

    一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...