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的更多相关文章

随机推荐

  1. hdu - 1704 Rank(简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1704 遇到标记过的就dfs,把隐含的标记,最后计数需要注意. #include <cstdio> # ...

  2. [bzoj2962]序列操作_线段树_区间卷积

    序列操作 bzoj-2962 题目大意:给定一个n个数的正整数序列,m次操作.支持:1.区间加:2.区间取相反数:3.区间求选c个数的乘积和. 注释:$1\le n,m\le 5\cdot 10^4$ ...

  3. Ubuntu 16.04安装Memcached(单机)

    Ubuntu 16.04安装Memcached,不过不仅限与Ubuntu,可以用CentOS等去安装,只不过测试时使用的是Ubuntu机器.Windows下不建议使用,本机调试可以使用,线上环境除了W ...

  4. 几点平时不太注意的CSS知识

    1:文本显示的时候,我们发现左右参差不齐,text-align:justify  就能让文本左右都齐刷刷的啦: 2:input标签的内容,处于安全考虑,有时候我们并不希望别人黏贴复制,这时候这样干:& ...

  5. git锁和钩子以及图形化界面

    1.锁机制 Locking Options 严格锁(strict locking):一个时刻,只有一个人可以占用资源. 乐观锁(optimistic locking):允许多个人同时修改同一文件.乐观 ...

  6. Creating A Simple Web Server With Golang

    原文:https://tutorialedge.net/post/golang/creating-simple-web-server-with-golang/ -------------------- ...

  7. NA远程

    远程网络按照L1分类:     租用专线(Leased Line):一般采用同步串行链路,使用HDLC/PPP封装:     线路交换(Circuit-Switched):一般采用异步串行链路,使用H ...

  8. 七夕节 看到很多停止更新的blog 莫名有点淡淡的忧桑

    又是一年七夕.又是一年单身.看到很多停止更新的blog, 仿佛看到了一茬一茬的程序猿 进入it 圈 又离开it圈,就有莫名的忧桑

  9. Start Xamarin——与Microsoft 的sales development manager的闲谈

    由于在Xamarin属于微软之前,就已经有Xamarin的账号,试用过破解版的.所以4月1号微软set Xamarin free之后.就收到了Xamarin的邀请试用邮件. 试用完了之后第二天.收到邮 ...

  10. iOS项目开发实战——通过Http Get方式与server通信

    移动client往往须要同后台server进行通信,上传或者下载数据,最经常使用到的方式就是Http Get,如今我们来学习在iOS项目中使用Get方式同server进行通信. [一]server端实 ...