`

ACM 3n+1的问题

    博客分类:
  • ACM
阅读更多
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


My c code:
#include<stdio.h>
int main()
{
unsigned long a,b,i,max,cycle,c,n;
while(scanf("%ld%ld",&a,&b)!=EOF)
{
printf("%ld %ld",a,b);
if(a>b)
{
c=a;
a=b;
b=c;
}
max=0;
for(i=a;i<=b;i++)
{
n=i;
cycle=1;
while(n!=1)
{
n=n%2?(3*n+1):n/2;
cycle++;
}
max=max<cycle?cycle:max;
}
printf(" %ld\n",max);
}

return 0;
}
分享到:
评论

相关推荐

    ACM集训+真题+讲解+ACM集训+真题+讲解

    ACM集训+真题+讲解+算法+代码模板库+视频 ACM集训+真题+讲解+算法+代码模板库+视频 ACM集训+真题+讲解+算法+代码模板库+视频 ACM集训+真题+讲解+算法+代码模板库+视频 ACM集训+真题+讲解+算法+代码模板库+视频 ...

    100 3N+1 以空間換時間

    叭啦 叭啦 巴拉 就是ACM100 3N+1 巴拉巴拉巴拉 叭啦 叭啦 巴拉 就是ACM100 3N+1 巴拉巴拉巴拉

    ACMC++程序设计题

    ACMC++代码,有程序题目及代码,还有一些函数的应用

    ACM数学+数论大全

    一-BASIC 3 IO挂 3 快速乘法 3 快速幂 3 进制转换(包括负进制)概念 3 一个数二进制1的个数 3 ...筛可以表示成x^2+(x+1)^2的素数 9 高斯整数环与高斯素数 10 n!中素数y的个数 10 筛1~n的因子个数O(n) 10

    ACMC++算法模板速记【代码+注释】

    内容概要:这个资源是为ACM竞赛准备的C++算法模板速记,旨在帮助竞赛选手快速掌握常用的算法模板和技巧,提高解题效率。资源中包含了常用的数据结构和算法实现,例如栈、队列、堆、图论算法、动态规划等,并配有详细...

    杭电 acmA + B Problem

    在编程的时候供大家参考,希望各位参加 acm的同学好好努力,能够取得好成绩

    ACM模板 + 经典题目 + 比赛试题 -代码实现

    竞赛需求: 在如ACM国际大学生程序设计竞赛等赛事中,参赛者需要在有限的时间内解决多个复杂的编程问题。一个良好的起点对于提高开发效率和达到更好的成绩至关重要。 模板作用: ACM模板项目通过提供一个统一的、经过...

    ACM题目+大部分答案

    都是自己编写,可以参考一下,部分是C语言的,供学习,欢迎留言一起学习

    ACM+计算几何+必看.ppt

    ACM+计算几何+必看.pptACM+计算几何+必看.ppt

    杭电 ACM

    杭电 ACM

    acm竞赛第100题答案

    acm第100题 The 3n+1 problem #include &lt;iostream&gt; using namespace std; unsigned CyCle(unsigned m) { unsigned count = 1; while (m != 1) { if (m & 0x01) m = 3 * m + 1; else m = m / 2; ...

    ACM所有函数模板

    acm所有函数的模板。包括各种排序,并查集,普利姆,克鲁斯卡尔算法,数据结构的,图论,数论,欧几里得算法等等。ACMER,加油吧!

    ACM打包资料

    里面是些代码,主要介绍些ACM编程的技巧和一些代码演示。欢迎下载哦。

    ACM+算法集--常用ACM算法

    ACM+算法集--常用ACM算法. 代码模板!!!

    acm备考资料整理(比较全面,包括各种算法)

    acm备考资料整理(比较全面,包括各种算法) 个人整理了很久的资料

    ACM高精度计算,非常详细适合入门

    ACM高精度计算,非常详细适合入门 ACM高精度计算,非常详细适合入门

    ACM 模板 函数

    ACM 模板 函数

    C++ ACM 离线题库超级全

    C++ ACM 离线题库超级全。 超级多的题库有离线的适合没网时做,ACMer必备,有杭电OJ,北大OJ ACM 离线题库

    ACM_DFS+BFS

    ACM_DFS+BFS

Global site tag (gtag.js) - Google Analytics