题解报告: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 ...
随机推荐
- 【Nginx】I/O多路转接之select、poll、epoll
当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一个描述符上面,另外的描述符虽然有数据但是不能读出来,这样实时性不能满足要求,大概的解决方案有以下几种: 1.使用多进程或 ...
- php.ini中extension默许的地址到底在哪里设置的
原文: http://www.myexception.cn/php/1436096.html ----------------------------------------------------- ...
- RHEL 启动系统及故障排除
一:Linux的启动过程: 开机加电自检->MBR引导(boot loader占446字节,分区列表64字节,magic占2字节)-->grub菜单(MBR是grub的第一个字段,第二个字 ...
- URL传参中文乱码的一种解决方法
中文乱码是由于,发送和接收方使用的编码解码格式不一致导致,以下是关于url传参解决中文乱码的一种方法,最后根据各种编码格式尝试解码,发现正确的解码格式 string strQueryString = ...
- MySQL服务无法启动(1067)问题
关于这个问题网上的帖子和说法多如牛毛,是在难以分辨真假,或者是否与自己的出错情况相同. 有了前车之鉴,就有必要提前声明,这篇是我在计算机--管理--服务中启动mysql服务时出现的错误,如下: 最后的 ...
- mysql 时间间隔计算
业务需求说明 早8:30点执行该sql,写入定时脚本 [在稳定前,需要手动复检] INSERT INTO test_error ( url, no_open_times, no_ad_times, o ...
- mac下Android Studio干净卸载
1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/Pr ...
- 关于mysqld_safe
昨天花了一天时间写了mysql的源码安装,比较蛋疼.其中对于mysqld_safe尤其不理解,因为使用apt-get安装几乎中间不需要什么配置,只需要service mysql start即可,但是源 ...
- 网页音乐播放器javascript实现,可以显示歌词
可以显示歌词,但是歌词和歌曲都要实现自己下载下来.只能播放一首歌,歌词还得是lrc格式的代码写的很罗嗦,急切希望帮改改CSS的代码1.代码:<html > <head> ...
- NSString字符串截取方法
1.字符串 1> 字符串比较 NSString *a = @“hello”; NSString *b = [NSString stringWithFormat:@hello”]; if (a = ...