题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032
Problem Description
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.
Input
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.
您应该处理所有整数对,并为每对确定i和j之间(包括i和j之间的所有整数)的最大周期长度。
您可以假定没有操作溢出32位整数。
Output
Sample Input
Sample Output
解题思路:这道题的意思就是输入一个区间i,j找出这里面的最长周期;周期是这样子:当这个数(不为1)是奇数时就变为3*n+1,为偶数时就变为n/2,并且用sum来计数周期,直到n为1就跳出。(水题!!!注意杭电oj出题的一些坑)
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,sum,t,maxn;
bool f=false;//标记是否交换了
while(cin>>a>>b){
if(a>b){
swap(a,b);//题目的陷井
f=true;
}
maxn=;
for(int i=a;i<=b;i++){
sum=,t=i;
while(t!=){
if(t%)t=*t+;
else t/=;
sum++;
}
maxn=max(sum,maxn);
}
if(f){
swap(a,b);//题目有说保持原来的数据输出,所以还得交换过来
f=false;//同时置f为false
}
cout<<a<<' '<<b<<' '<<maxn<<endl;
}
return ;
}
题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)的更多相关文章
- 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【注意细节】【预计数据不强】【8月21】
The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...
- 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 ...
- 题解报告:poj 2480 Longge's problem(欧拉函数)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- 题解报告:hdu 2069 Coin Change(暴力orDP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Problem Description Suppose there are 5 types of ...
- 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
随机推荐
- Meteor结构
Meteor提供了一些特殊的文件夹,可以帮助开发人员构建他们的应用程序. client 如果创建客户端文件夹,这个文件夹里面的一切都将在客户端上运行.在这里,可以将您的HTML,CSS和客户端Java ...
- 【Nginx】Nginx的配置
配置文件为.conf文件 一.块配置项 块配置项由一个块配置项名和一对大括号组成.具体如下: events{ ... } http{ upstream backend{ server 127.0.0. ...
- 【Nginx】请求上下文
上下文与全异步web服务器的关系 请求上下文指在一个请求的处理过程中,把一些关键的信息保存下来的类似struct这样的结构体.每个http模块都可以有自己的上下文结构体,一般都是在刚开始处理请求时在内 ...
- curl 中文乱码
curl 中文乱码 学习了:https://blog.csdn.net/thc1987/article/details/52583789 学习了: http://blog.itpub.net/2903 ...
- [Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript
In this lesson we are going to setup a project from scratch by introducing the JavaScript snippet to ...
- Activity调用isDestroyed()方法报出,java.lang.NoSuchMethodError
在測试App的过程中,Activity调用了isDestroyed()方法,报出了java.lang.NoSuchMethodError错误. 自己手机MI 2S,版本号4.1.1. 事实上原因就是i ...
- 微信小程序之 Classify(商品属性分类)
1.项目目录 2.逻辑层 broadcast.js // pages/broadcast/broadcast.js Page({ /** * 页面的初始数据 */ data: { firstIndex ...
- 深入浅出AOP(一)
动态代理实现AOP: AOP事实上非常早之前依照做出来了一些东西,之所以不敢说做出来了.是由于它是什么?怎么实现?做出来的东西是不是?先前一直查资料.查到的资料跟着做.到后来发现,AOP越来越大,而非 ...
- 【Mongodb教程 第九课 】MongoDB 删除文档
remove() 方法 MongoDB的 remove() 方法用于从集合中删除文档.remove() 方法接受两个参数.第一个是删除criteria ,第二是justOne标志: deletion ...
- 排队理论之性能分析 - Little Law & Utilization Law
了解一个系统的性能一般是參考一些度量值(Metric),而怎样计算出这些Metric就是我们要讨论的.Little Law(排队理论:利特儿法则)和Utilization Law是Performanc ...