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。
      代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int inline countTimes(int n)
  6. {
  7. int times = 1;
  8. do
  9. {
  10. if(n & 0x1 == 1)
  11. {
  12. n = 3 * n + 1;
  13. }
  14. else
  15. {
  16. n = n >> 1;
  17. }
  18. ++times;
  19. }while(n != 1);
  20. return times;
  21. }
  22. int main()
  23. {
  24. int i, j;
  25. while (scanf ("%d%d", &i, &j) != EOF)
  26. {
  27. int bi1, bi2, bi3;
  28. if (i > j)
  29. {
  30. bi1 = j;
  31. bi2 = i;
  32. }
  33. else
  34. {
  35. bi1 = i;
  36. bi2 = j;
  37. }
  38. int maxTimes;
  39. maxTimes = countTimes (i);
  40. for (int k = bi1 + 1; k <= bi2; k++)
  41. {
  42. bi3 = countTimes(k);
  43. if (bi3 > maxTimes)
  44. maxTimes = bi3;
  45. }
  46. cout << i << ' ' << j << ' ' << maxTimes << endl;
  47.  
  48. }
  49. system ("pause");
  50. return 0;
  51.  
  52. }

杭电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. Redis实战(四)

    配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息. 接下来,就可以进行Redis的数据写入了.Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具 ...

  2. 专业的抢票软件 12306bypass

    专业的抢票软件 https://www.12306bypass.com/

  3. bean的singleton(没有看到生命周期范围??)

    4.5.1 The singleton scope Only one shared instance of a singleton bean is managed, and all requests ...

  4. 路径方案数 [SPFA,拓扑排序]

    路径方案数 [题目描述] 给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点, cyb 可以从 a 走到 b,当且仅当 a 到 2 的最短路,比 b 到 2 的最短路长. 求 ...

  5. platform 模块

    python中,platform 模块给我们提供了很多方法去获取操作系统的信息,如: import platform platform.platform() #获取操作系统名称及版本号 'Window ...

  6. 最短路——spfa

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  7. Unity 2D游戏开发教程之游戏精灵的开火状态

    Unity 2D游戏开发教程之游戏精灵的开火状态 精灵的开火状态 “开火”就是发射子弹的意思,在战争类型的电影或者电视剧中,主角们就爱这么说!本节打算为精灵添加发射子弹的能力.因为本游戏在后面会引入敌 ...

  8. JS延迟执行

    <!DOCTYPE html> <html> <head> <title></title> <script type="te ...

  9. hiho1291(逆序思维,并查集)

    题目链接:[https://hihocoder.com/problemset/problem/1291] 题意:在<我的世界>游戏中放置沙盒,沙盒为体积为1的正方体,按顺序给你一些坐标,然 ...

  10. 【BZOJ 3812】 3812: 主旋律 (容斥原理**)

    3812: 主旋律 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 235  Solved: 196 Description 响应主旋律的号召,大家决定 ...