The 3n + 1 problem

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. 

 
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.

 
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). 

 
Sample Input
1 10
100 200
201 210
900 1000
 
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
 
Source
UVA
 
Recommend
mcqsmall
 
       
 问题直接用暴力法就可以解出来,唯一要注意的一点就是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的更多相关文章

  1. 题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Computer Science ...

  2. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  3. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  4. 杭电oj 2095 & 异或^符号在C/C++中的使用

    异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...

  5. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

  6. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

  7. 杭电oj————2057(java)

    question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...

  8. 爬取杭电oj所有题目

    杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...

  9. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

随机推荐

  1. PhpStorm函数注释的设置

    首先,PhpStorm中文件.类.函数等注释的设置在:setting->Editor->FIle and Code Template->Includes下设置即可,其中方法的默认是这 ...

  2. Linux的权限对于文件与目录的意义

    权限对文件: r:可读取此文件的实际内容. w:可以编辑.新增或者是修改该文件的内容(但不含删除该文件),如果没有r权限,无法w. x :该文件具有被系统执行的权限.可以删除. 权限对目录: r:re ...

  3. bzoj 1880 最短路径图

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  4. 三 Python解释器

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  5. SQL join关键字

    如果一张表有很多个字段可能填入起来十分的困难复杂,不如把它拆分成两个表,然后查看的时候合并起来. 比如我要记录学生的姓名,班级,成绩,父母的电话号码,那么我们可以创建一个表1 储存学生的姓名班级成绩, ...

  6. ref:学习笔记 UpdateXml() MYSQL显错注入

    ref:https://www.cnblogs.com/MiWhite/p/6228491.html 学习笔记 UpdateXml() MYSQL显错注入 在学习之前,需要先了解 UpdateXml( ...

  7. 洛谷P3402 【模板】可持久化并查集 [主席树,并查集]

    题目传送门 可持久化并查集 n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 ...

  8. openwrt 控制gpio口的方法

    利用i2c来控制gpio口 1.编译驱动或者安装驱动 # opkg update # opkg install kmod-i2c-gpio-custom kmod-i2c-core# 加载驱动 # i ...

  9. Unity Shader 之 uv动画

    Unity 动画 Unity Shader 内置时间变量 引入时间变量 名称 类型 描述 _Time float4 t是自该场景加载开始所经过的时间,4个分量分别是(t/20, t, 2t, 3t) ...

  10. python tesseract 识别图片中的文字的乱码问题(ubuntu系统下)

    OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程. 首先,需要安装 tesseract-ocr(tesseract O ...