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的更多相关文章
随机推荐
- 光纤通信(codevs 1955)
题目描述 Description 农民John 想要用光纤连通他的N (1 <= N <= 1,000)个牲口棚(编号1..N).但是,牲口棚位于一个大池塘边,他仅可以连通相邻的牲口棚.J ...
- 2018/2/14 x-pack的学习
x-pack是什么?它能提供的作用如下,下面描述的这些功能都属于x-park:Shield: 提供对数据的 Password-Protect,以及加密通信.基于角色的权限控制,IP 过滤,审计,可以有 ...
- Spring3.2+mybatis3.2+Struts2.3整合配置文件大全
0.配置文件目录 1.Spring配置 applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8 ...
- 守护进程详解及创建,daemon()使用
一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而 且提供某种服务,不是对整 ...
- Linux下汇编语言学习笔记42 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- CentOS虚拟机与本机同步时间
接着之前的任务,还是为了在VMWare上搭建分布式hadoop集群.搭着搭着注意到虚拟机上的时间和本机是不同步的,而且可以说是乱七八糟,3台虚拟机时间都与本机差了8个小时以上.首先确认不是时区的问题, ...
- 欧拉 路径&&回路
不管 欧拉回路 还是 欧拉路径 无向图或者有向图(删除方向后)要联通 欧拉路径存在的判定条件 1 无向图 度数为奇数的点最多有两个 2 有向图 最多只能有两个点的入度不等于出度 ...
- P1160 队列安排 洛谷
https://www.luogu.org/problem/show?pid=1160 题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进 ...
- Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后启动菜单的System Setup选项提示:can't find command "fwsetup"
背景: 硬盘分区方式:MBR 硬盘容量256,Windows 100,Ubuntu 156,其中主分区安装的是Windows,Ubuntu安装在逻辑分区上,文件系统为Ext4,整个Ubuntu就挂载在 ...
- tsdb import 相关
今天一直在做opentsdb 大量导入数据的工作. 中间遇到了一些值得记录的问题, 这里随手记一下 明天好好整理 1. 多进程logger python的logging模块不支持多进程,但我们可以用s ...