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 ...
随机推荐
- PhotoKit详解
Photokit介绍 这篇主要介绍如何通过 Photokit获取数据 photokit.jpg 1,基类 PHObject Photos 框架中的根类PHObject只有一个公开接口 localIde ...
- JavaScript点击事件-一个按钮触发另一个按钮
<input type="button" value="Click" id="C" onclick="Go();" ...
- sublime运行Python
1.首先安装Python 我这里安装的是Python的3.7版本. 这里有两种安装方式 第一种: 默认路径安装,勾选添加到path复选框(这种情况,sublime可以直接运行Python了) 第二种: ...
- python3的时间日期处理
1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时 ...
- mongodb 的注意点
昨天同事安装mongodb遇到了些问题,问了下我,后拉发现都是些细节没注意(讲道理这应该是很简单,一顿操作就ok的事情) 首先,下载 mongo包, 然后 ,解压安装, 启动之. 问题就出现在他后台启 ...
- 4G U盘版64位bitcoin专用挖矿操作系统
这个操作系统是基于linux的操作系统,采用的ubuntu平台打造,所有的软件都已经安装齐备,是一个bitcoin专用挖矿操作系统,是64位的,对于显卡数量基本上没有限制,前提是你的主板支持足够多的显 ...
- iOS block 闭包的学习
iOS 闭包 学习 理解: 1 . 闭包外界无法访问内部变量 ,它是一个独立的代码块. 2 . 闭包可以作为 一个方法 ,甚至局部变量 全局 变量 3 . 闭包 是一种引用类型 注 ...
- [原创] hadoop学习笔记:重新格式化HDFS文件系统
所谓的重新格式化HDFS文件系统,实际意味着重新的创建一个HDFS文件系统.也就是说,必须将先前的已经有的文件系统配置删除.如下: 笔者采用的是最小化安装 这个是core-site.xml配置 这个是 ...
- android OTG【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7707056 一.OTG的概念 OTG是On-The-Go的缩写,是近年发展起来的技术, ...
- 修改push动画的方向
CATransition *animation = [CATransition animation]; animation.duration = 0.4; animation.timingFuncti ...