ACM_Hailstone HOTPO
Hailstone HOTPO
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
The hailstone sequence is formed in the following way:
(1) If n is even, divide it by 2 to get n'
(2) If n is odd, multiply it by 3 and add 1 to get n'
It is conjectured that for any positive integer number n, the sequence will always end in the repeating cycle: 4, 2, 1, 4, 2, 1,... Suffice to say , when n == 1, we will say the sequence has ended.
Write a program to determine the largest value in the sequence for a given n.
译文:以下面的方式形成在冰雹序列:
(1)如果n为偶数,除以2以获得N“
(2)如果n为奇数,乘以3,并添加1以获得N”
据推测,对于任何正整数n,序列将总是以重复周期结束:4,2,1,4,2,1,......只要n == 1,我们就会说序列已经结束。
编写一个程序来确定给定n的序列中的最大值。
Input:
The first line of input contains a single integer P, (1<= P <= 100000), which is the number of data set s that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input consisting of two space separated decimal integers. The first integer is the data set number. The second integer is n, (1 <= n <= 100,000), which is the starting value.
译文:第一行输入包含一个整数P,(1 <= P <= 100000),它是后面的数据集的数量。每个数据集应该被相同和独立地处理。
每个数据集由一个由两个空格分隔的十进制整数组成的单行输入组成。第一个整数是数据集编号。第二个整数是n,(1 <= n <= 100,000),这是起始值。
Output:
For each data set there is a single line of output consisting of the data set number, a single space, and the largest value in the sequence starting at and including n.
译文:对于每个数据集,都有一行输出,其中包含数据集编号,单个空间以及从n开始的序列中的最大值。
Sample Input:
4
1 1
2 3
3 9999
4 100000
Sample Output:
1 1
2 16
3 101248
4 100000
解题思路:题目很简单,就是找序列中的最大值,按规则来查找。我们知道序列中要么是奇数要么是偶数,因而在循环中判断一下是否为奇数,若是,执行规则(2)后也是变为偶数,所以容易想到循环体内最后直接执行n/=2;加上查找序列中最大值的判断语句,水过。说明:题目中输入的个数p最多就有10^5个,假设有p个n是接近10^5,那么求序列中的最大值可能就会超时,因为循环节长度是不知道的,有可能更多的循环次数,所以后台数据有点水,这题真正的解法应该不是这样的,让菜鸡想想,待想到再回来修改!
贴一下水数据的AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int p,m,n,maxn;
cin>>p;
while(p--){
cin>>m>>n;
maxn=n;
while(n!=){
if(n%)n=n*+;
if(maxn<n)maxn=n;
n/=;
}
cout<<m<<' '<<maxn<<endl;
}
return ;
}
杭电hdu4484与此题一样,题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4484
ACM_Hailstone HOTPO的更多相关文章
随机推荐
- 2018/3/4 Activiti教程之对于流程的基本操作以及从发起到完成还有相关注意事项(与Springboot整合版)三
写教程实在太累了,,,还浪费时间,Activiti教程就写到这好了,不过最近在玩区块链,到时候写几个区块链方面的教程. 这是一些流程的查询与删除api,删除这块,默认是级联删除(加个false参数,就 ...
- Linux下汇编语言学习笔记16 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Delphi:校验手机号及身份证号
//校验手机号 function IsMobileNumber( num:string ):boolean; begin Result:=False; if length( tr ...
- 用 字蛛 取出需要的字符应用字体 @font-face
一.安装font-spider npm install font-spider -g 二.目录结构 font-spider font FZZZHONGHJW.ttf font.html 三.font. ...
- MongoDB小结17 - find【查询条件$or】
我们再添加一个游泳的人,并用$in查询游泳的人 db.user.find({"hobby":{"$in":["swimming"]}},{& ...
- 【转】实现一个自己的promise
转, 原文:http://blog.csdn.net/yibingxiong1/article/details/68075416------------------------------------ ...
- 003 rip
r0#config t Enter configuration commands, one per line. End with CNTL/Z. r0(config)#router rip r0(c ...
- Java总结之网络
[网络基础概念] 什么是计算机网络: 把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大.功能强的网络系统,从而使众多的计算机能够方便的互相传递信息,共享硬件.软件.数据信息等资源 ...
- 怎样在Swift中使用NSError
步骤一:声明NSError变量. 一定要加"?",不加或者加"!"都不行.由于使用了optional,所以要用var而不用let. var error: NSE ...
- 软考之J2SE
特别感谢软考让我如今就接触了神奇的java.曾经尽管真不知道java是个神马,看完马士兵的视频发现里面的东西并不陌生.有vb,c++,c#做基础加上这次的J2SE发现原来编程语言有非常多同样的特性.也 ...