Poj1207 The 3n + 1 problem(水题(数据)+陷阱)
一、Description
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
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
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(水题(数据)+陷阱)的更多相关文章
- hdu-5867 Water problem(水题)
题目链接: Water problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Codeforces - 1194B - Yet Another Crosses Problem - 水题
https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...
- poj 1658 Eva's Problem(水题)
一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...
- poj-1207 THE 3n+1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of pro ...
- HDU 5832 A water problem 水题
A water problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...
- bestcoder 48# wyh2000 and a string problem (水题)
wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K ...
- Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题
C. NP-Hard Problem 题目连接: http://www.codeforces.com/contest/688/problem/C Description Recently, Pari ...
- 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 ...
- 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 ...
随机推荐
- 洛谷2704 [NOI2001]炮兵阵地
题目戳这里 Solution 状压DP很好的入门题,用熟练位运算貌似也没那么难. 首先分析一下题目: 看见n=100,m=10,立马就想到了状压,看起来也像DP,所以我们还是采用行号为阶段的状压DP. ...
- PoC简介
无线一键通功能,POC(PTT Over Cellular)也称PTT(Push To Talk)功能.PTT:一键通(Push-to-Talk)功能是一种全新的移动技术,可以快速地进行"一 ...
- Bootstrap第3天
Bootstrap第3天 图片样式 .img-responsive:直接为图片添加该样式,可以实现响应式图片. .center-block:图片居中样式,而不能使用text-center样式. 图片形 ...
- sublime 快捷键,左菜单乱码
sublime 快捷键安装 packagecontrol https://packagecontrol.io/installation#st3 import urllib.request,os,has ...
- Power Designer体验之旅
版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/wang13667539325/article/details/36025245 从某种程度上说.不论 ...
- MSSQL触发器
1.触发器语法 CREATE TRIGGER<trigger name> ON [<模式名>.]<表名或视图名> [WITH ENCRYPTION] {{{FOR| ...
- 3.15课·········out传值(传址)
public void Hs(out int a, out int b) { a = 4; b = 6; a = b++;//a=6,b=b+1=7//b先赋值给a,然后b+1 b = ++a;//a ...
- js之定时器
一.通过定时器我们可以间隔设定时间重复调用某个函数,利用这个特性,我们可以做很多事,例如,12306上的每间隔5秒查询自动查询一次余票,简单动画的实现等等 二.定时器的格式: 定时器有两种格式,分别是 ...
- Spark Structured Streaming框架(1)之基本用法
Spark Struntured Streaming是Spark 2.1.0版本后新增加的流计算引擎,本博将通过几篇博文详细介绍这个框架.这篇是介绍Spark Structured Streamin ...
- 20165101刘天野 2017-2018-2 《Java程序设计》第1周学习总结
20165101刘天野 2017-2018-2 <Java程序设计>第1周学习总结 教材学习内容总结 Java的地位 Java的特点 安装JDK Java应用程序开发步骤 反编译 安装Gi ...