1410131848-hd-The 3n + 1 problem

来源:互联网 发布:西岐网络 编辑:程序博客网 时间:2024/05/20 16:13

The 3n + 1 problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22622    Accepted Submission(s): 8477


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 10100 200201 210900 1000

Sample Output
1 10 20100 200 125201 210 89900 1000 174
题目大意
      按照题目给出的规则,求一段数据之中转换次数最多的那一个。
代码
#include<stdio.h>#include<string.h>int num[1100000];int main(){int i,j;int a,b;int c,d,e,max;while(scanf("%d%d",&i,&j)!=EOF){memset(num,0,sizeof(num));//记得每次都需要将数组清零,要不然每次的数据都会影响下一次 if(i<j){a=i;b=j;}else{a=j;b=i;}//注意给出的两个数不一定是按照字典续给出的 max=-1;for(c=a;c<=b;c++)    {    e=c;    while(e!=1)    {    if(e%2==0)        e/=2;    else        e=3*e+1;    num[c]++;    }    num[c]+=1;//加上本身     if(max<num[c])        max=num[c];    }printf("%d %d %d\n",i,j,max);}return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 联通斐讯路由器返现被骗了怎么办 消费分期后退款分期账单还在怎么办 新房装修物业电梯用不了费用怎么办 在京东拼购没有拼成已付款的怎么办 微信上买东西发的货不一样怎么办 微信买东西收到货不付款怎么办 京东商品店家待出库不发货怎么办 微信二维码付款多付了怎么办 微信二维码付款付错了怎么办 身份证被移动公司拉黑了怎么办 微店申请退款卖家不同意怎么办 淘宝退货快递把我名字填错了怎么办 刚申请淘宝店信用为零怎么办 淘宝买家已付款卖家不做皮单怎么办 淘宝店铺被屏蔽7天后该怎么办 香信得登录密码忘了怎么办 苹果手机迅雷下载不了的资源怎么办 快手官方私信你的作品违规了怎么办 顺丰生鲜速配食物坏了怎么办 半年汽车没有年检 交警抓到怎么办 没年检的车子被交警抓到怎么办 去年检的路上被交警抓了怎么办 微信聊天界面群聊删除找不到怎么办 微博抽奖的奖品没发货怎么办 两个微信号绑了一个手机号怎么办 欧月玫瑰花朵叶子上有白粉怎么办 进对方空间被挡不想让他知道怎么办 qq上买东西给钱了对方没给怎么办 微信钱包手势密码忘记了怎么办 买家一起拍了两件宝贝怎么办 宿雾航空付款无法显示验证码怎么办 不小心把购物车的东西删了怎么办 微信在别人电脑登录忘退出怎么办 微信电脑版忘了退出怎么办 给微商交了定金不给退怎么办 方舟手游飞龙驯服时间不够怎么办 淘宝上买了假货找不到商家怎么办 魅蓝手机一直在开机画面怎么办 在实体店买的手机想退怎么办 淘宝买了东西发货了不想要了怎么办 快递已经发货了不想要了怎么办