题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032

Problem Description

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
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.
译文:计算机科学中的问题通常被归类为属于某类问题(例如,NP,Unsolvable,Recursive)。在这个问题中,您将分析一个算法的属性,该算法的分类对于所有可能的输入都是未知的。
考虑以下算法:
1.输入n
2.打印n
3.如果n = 1则停止
4.如果n为奇数,则n <-3n + 1
5.否则n < - n / 2
6.转到2 
给定输入22,将打印下列数字序列22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
据推测,上述算法将终止(当打印1时)任何积分输入值。尽管算法很简单,但是这个猜想是否属实却是未知数。然而,已经证实,对于所有整数n使得0 <n <1,000,000(事实上,对于比这更多的数字)
给定输入n,可以确定打印的数字的数量(包括1)。对于给定的n,这称为n的周期长度。在上面的例子中,22的周期长度是16. 
对于任何两个数字i和j,您应确定i和j之间所有数字的最大周期长度。

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0. 
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对,每行一对整数。所有整数将小于1,000,000且大于0. 
您应该处理所有整数对,并为每对确定i和j之间(包括i和j之间的所有整数)的最大周期长度。
您可以假定没有操作溢出32位整数。

Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output 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). 
译文:对于每对输入整数i和j,您应该输出i,j以及i和j之间(包括i和j)之间的整数的最大周期长度。这三个数字应该由至少一个空格分隔,所有三个数字在一行上并且每行输入使用一行输出。整数i和j必须以它们出现在输入中的相同顺序出现在输出中,并且后面应跟着最大循环长度(在同一行上)。

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

解题思路:这道题的意思就是输入一个区间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(克拉兹问题)的更多相关文章

  1. HDU 1032 The 3n + 1 problem (这个题必须写博客)

    The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】

    The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...

  3. HDU 1032 The 3n + 1 problem

    还以为要递归推一推的 结果暴力就过了 要注意 i,j 大小 #include <iostream> using namespace std; int a,b; long long cnt, ...

  4. 杭电OJ——1032 The 3n + 1 problem

    The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. 题解报告: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 ...

  7. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  8. 题解报告:hdu 2069 Coin Change(暴力orDP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Problem Description Suppose there are 5 types of ...

  9. 题解报告: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 ...

随机推荐

  1. SecureCRT5 中文乱码

    SecureCRT5 中文乱码 secureCRT7已经不用这样设置了: 学习了:http://www.iitshare.com/securecrt-chinese-garbled-solution. ...

  2. Java多线程导致的的一个事物性问题

    业务场景 我们如今有一个类似于文件上传的功能.各个子网站接受业务,业务上传文件,各个子网站的文件须要提交到总网站保存.文件是按批次提交到总网站的,也就是说,一个批次以下约有几百个文件. 考虑到白天提交 ...

  3. 在DIV中自己主动换行

    word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自己主动换行. 它们的差别就在于: 1,word-break:break-all 比如di ...

  4. Failed to load resource: the server responded with a status of 404 (Not Found)

    Failed to load resource: the server responded with a status of 404 (Not Found) 报错情况:图标加载失败 原因分析:路径错误 ...

  5. Java基础实例

    打印等腰三角形代码 public class ForForTest{ public static void main(String []args){ for(int x=0;x<5;x++){ ...

  6. 【CODEFORCES】 C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. css3最新版中文参考手册在线浏览

    对于CSS 3.0,它对于我们Web设计人员来说不只是新奇的技术,更重要的是这些全新概念的Web应用给我们的设计开发提高了效率以及更多的无限可能性,我们将不必再依赖图片或者 Javascript 去完 ...

  8. Windows 异步IO操作

    Windows提供了4种不同的方法来接收I/O请求已经完成的通知:触发设备内核对象.触发事件内核对象.可提醒I/O和I/O完成端口. Windows的异步I/O     当线程向设备发起一个I/O异步 ...

  9. ubuntu切换中英文通用方法,ubuntu中文语言

    1:点击桌面右上角的齿轮,选择“system settings”进入系统设置界面

  10. The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.

    编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...