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 ...
随机推荐
- zoj 3716 Ribbon Gymnastics【神奇的计算几何】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3716 来源:http://acm.hust.edu.cn/vjudg ...
- 2017-2018-1 20179209《Linux内核原理与分析》第十周作业
设备与模块 设备分类 块设备 块设备可以以块为单位寻址,块大小随设备不同而不同:设备通常支持重定位操作,也就是对数据的随机访问.块设备的例子有外存,光盘等. 字符设备 字符设备不可寻址,仅供数据的流式 ...
- zip filter map 列表生成器
map map(function, list): 就是对list 中的每一个元素都调用function函数进行处理,返回一个map的对象 list一下就可以生成一个列表 或者for循环该对象就可以输出 ...
- git本地仓库管理远程仓库
git remote add origin https://xxxxxgit push -u origin master
- 教你管理SQL数据库系列(1-4)
原文 教你管理 SQL Server 数据库(1)数据库的结构 http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...
- ubuntu 安装vagrant过程
Ubuntu安装vagrant时需要首先安装virtualBox. Step1: 在https://www.virtualbox.org/wiki/Linux_Downloads 下载ubuntu对应 ...
- zend 和 esftp插件开发大型PHP项目,ZEND最常用快捷键
先说一下如何安装zend的esftp插件,下载插件esftp-1.1.1.zip,下载地址http://sourceforge.net/projects/esftp/ 或者 http://yun.ba ...
- 第12条:不要在for和while循环后面写else块
核心知识点: (1)一般的if/else是前面不执行,后面才执行,循环下面的else是前面执行完后面才会执行,如果是break打断也不会执行.循环为空或False也不执行. (2)try/expect ...
- Swap file "/etc/.hosts.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it,
非正常关闭vi编辑器时会生成一个.swp文件 非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生 ...
- Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...