杭电OJ——1032 The 3n + 1 problem
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.
100 200
201 210
900 1000
100 200 125
201 210 89
900 1000 174
问题直接用暴力法就可以解出来,唯一要注意的一点就是i不一定小于j。
#include <iostream>
#include <cstdio>
using namespace std; int inline countTimes(int n)
{
int times = 1;
do
{
if(n & 0x1 == 1)
{
n = 3 * n + 1;
}
else
{
n = n >> 1;
}
++times;
}while(n != 1);
return times;
}
int main()
{
int i, j;
while (scanf ("%d%d", &i, &j) != EOF)
{
int bi1, bi2, bi3;
if (i > j)
{
bi1 = j;
bi2 = i;
}
else
{
bi1 = i;
bi2 = j;
}
int maxTimes;
maxTimes = countTimes (i);
for (int k = bi1 + 1; k <= bi2; k++)
{
bi3 = countTimes(k);
if (bi3 > maxTimes)
maxTimes = bi3;
}
cout << i << ' ' << j << ' ' << maxTimes << endl; }
system ("pause");
return 0; }
杭电OJ——1032 The 3n + 1 problem的更多相关文章
- 题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Computer Science ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...
- 杭电oj 2095 & 异或^符号在C/C++中的使用
异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...
- 用python爬取杭电oj的数据
暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...
- 杭电oj 4004---The Frog Games java解法
import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...
- 杭电oj————2057(java)
question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...
- 爬取杭电oj所有题目
杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
随机推荐
- es6js promise在ie中报错“未定义”
解决办法,我使用https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.auto.min.js直接引入在html中,也可以安装相应的babel-po ...
- QT构造函数中*parent
程序写的多了,你会发现几乎所有的Qt类的构造函数都会有一个parent参数.这个参数通常是QObject* 或者是 QWidget* 类型的(定义新的类是通常首先初始化为0,在类的实现函数中赋值).很 ...
- loadrunner乱码问题解决办法
7.LoadRunner回放脚本时,在浏览器显示的中文是乱码 最近,遇到了好多乱码的问题,解决了一些,还有最后一个乱码,能想到的各种办法都试过了,还是不行,很奇怪啊. 解决这些乱码时,涉及到了http ...
- 【C#】日期格式转换
C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了今天DateTime.Now.Date.ToShortDateString();昨天,就是今天的日 ...
- poj 2096Collecting Bugs
题目链接 poj 2096Collecting Bugs 题解 dp[i][j]表示已经找到i种bug,并存在于j个子系统中,要达到目标状态的天数的期望. 显然,dp[n][s]=0,因为已经达到目标 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- 洛谷P1462 通往奥格瑞玛的道路
题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...
- python开发_bisect
现在有如下的需求: ''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) ...
- python开发_email_读取邮件头信息
在python中的类库中,python自带了email模块. 在email模块中,我们可以email类库实现对邮件的读取,和邮件的发送等功能. 本文先来谈谈在python中,利用email模块读取邮件 ...
- hdu 4119 Isabella's Message 模拟题
Isabella's Message Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...