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. Network-POJ3694(最小公共祖先LCA+Tarjin)

    http://poj.org/problem?id=3694 这一题  为什么要找最小祖先呢 当两个节点连到一块的时候  找最小公共节点就相当于找强连通分支 再找最小公共节点的过程中直到找到  这个过 ...

  2. JSP的生命周期

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/life-cycle.html: JSP生命周期可以被定义为从创建到销毁的整个过程,这类似于一个Servl ...

  3. MongoDB小结26 - 地理空间索引

    现在有一种查询变得越来越流行(尤其是移动设备):找到离当前位置最近的N个场所. MongoDB专为平面坐标查询做了专门的索引,称为地理空间索引. 同样需要用ensureIndex创建,不过,参数是两个 ...

  4. CentOS 7 开启VNC Service

    由於是透過 GUI 管理, 所以需要圖形桌面環境, 如果沒有安裝, 可以用以下指令安裝 GNOME: # yum groupinstall “GNOME Desktop” Centos 7安裝 VNC ...

  5. 具体解释Android定位

    相信非常多的朋友都有在APP中实现定位的需求,今天我就再次超炒冷饭,为大家献上国内开发人员经常使用到的三种定位方式.它们分别为GPS,百度和高德,惯例先简介下定位的背景知识. 什么是GPS定位.基站定 ...

  6. MapReduce的Reduce side Join

    1. 简单介绍 reduce side  join是全部join中用时最长的一种join,可是这样的方法可以适用内连接.left外连接.right外连接.full外连接和反连接等全部的join方式.r ...

  7. Android--Activity在跳转时携带数据

    首先看看两种传递方法演示样例:(一个简单姻缘计算器) 主Activity import android.os.Bundle; import android.app.Activity; import a ...

  8. powerShell赋权限

    1.给网站赋权限 Set-SPUser –Identity “用户名” –AddPermissionLevel “参与讨论” –web “http://url” 2.给列表赋权限 $web = Get ...

  9. Appium、selenium与Robot Framework

    Robot Framework + Appium Appium-Python-Client: 与Appium Library和Appium Server的交互Appium Library通过Appii ...

  10. Java对话框总结

    总结起来非常easy: 1,对话框类型:消息.确认,选项.输入 2,选择图标:错误,信息.警告.问题,无或者自己定义 3,选择消息:字符串,图标.自己定义组件或者他们的集合 4.对于确认对话框,选择选 ...