HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】
The 3n + 1 problem
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 (including the 1). 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.
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.
You can assume that no opperation overflows a 32-bit integer.
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).
1 10
100 200
201 210
900 1000
1 10 20
100 200 125
201 210 89
900 1000 174
预计数据弱,不然直接做应该会超时(本来想写优化来着,没想到这题数据这么弱······)。
代码例如以下:
#include<cstdio>
int main(){
int n,m;
while(scanf("%d%d",&n,&m)==2){
int maxn=n>m?n:m;//没说n和m的大小,坑啊! int minx=n>m?m:n;
int maxx=0;
for(int i=minx;i<=maxn;i++){//总感觉这样会超时。看来数据不强啊
int x=i,sum=1;
while(x!=1){
if(x%2==0) x/=2;
else x=3*x+1;
sum++;
}
if(sum>maxx) maxx=sum;
}
printf("%d %d %d\n",n,m,maxx);
}
return 0;
}
HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】的更多相关文章
- HDU 1032 The 3n + 1 problem (这个题必须写博客)
The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Computer Science ...
- HDU 1032 The 3n + 1 problem
还以为要递归推一推的 结果暴力就过了 要注意 i,j 大小 #include <iostream> using namespace std; int a,b; long long cnt, ...
- 杭电OJ——1032 The 3n + 1 problem
The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...
- HDU 1031.Design T-Shirt【结构体二次排序】【8月21】
Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)
Problem A: The 3n + 1 problem Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 6[Submit][St ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53927 Accepted: 17 ...
- uva----(100)The 3n + 1 problem
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
随机推荐
- 面试准备——springboot相关
https://www.jianshu.com/p/63ad69c480fe https://blog.csdn.net/u013605060/article/details/80255192 htt ...
- [jenkins学习篇] 安装jenkins
1 下载war包,https://jenkins.io/download/ 2 安装jar包,java -jar jenkins.war 3 打开网址:http://localhost:8080 4 ...
- Axure:从单一评价方式到用户自由选择
导读: 亲,还记得淘宝对货物的评价方式吗?还记得对快递哥的评价方式吗? 1,经典五星评: ...
- TMOS_Order_of_Operations_v0.1
- 强大!不懂html也来学网抓(xmlhttp/winhttp+fiddler)
http://club.excelhome.net/thread-1159783-1-1.html 学习html强力推荐此网站:http://www.w3school.com.cn/ ,绝对权威 交流 ...
- jsp 详解request对象
request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1 object ...
- xml和数组互转
/** * 输出xml字符 * @param $params 参数名称 * return string 返回组装的xml **/ public function data_to_xml( $param ...
- 小机房的树(codevs 2370)
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- 从dataset表中获取某一列的所有值方法
原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] 可以datarow遍历所有行即可,如下:pubauthor这个表中的au_lname的所有值加到listbox上面 ...
- form:form的一点体会
留个日程吧,明个写,下班了>>