zoukankan      html  css  js  c++  java
  • The 3n + 1 problem

    The 3n + 1 problem

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

    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
     
    注意:没有说明m,n的大小,所以需判断
     
    HDU 1032果断水过,代码如下:
     1 #include <stdio.h>
     2 int main()
     3 {
     4     int m,n;
     5     while(scanf("%d %d",&m,&n)!=EOF)
     6     {
     7         int i,j,num,k,t=0;
     8         if(m>n)
     9         {
    10             for(i=n;i<=m;i++)
    11             {
    12                 j=i;
    13                 num=0;
    14                 while(j>1)
    15                 {
    16                     if(j&1)//位运算 判断 j 是否为奇数  
    17                     {j=(j*3+1)>>1;num+=2;}
    18                     else
    19                     {j>>=1;num+=1;}
    20                 }
    21                 if(num>t)
    22                 t=num;
    23             }
    24             printf("%d %d %d
    ",m,n,t+1);
    25         }
    26         else
    27         {
    28             for(i=m;i<=n;i++)
    29             {
    30                 j=i;
    31                 num=0;
    32                 while(j>1)
    33                 {
    34                     if(j&1)//位运算 判断 j 是否为奇数  
    35                     {j=(j*3+1)>>1;num+=2;}
    36                     else
    37                     {j>>=1;num+=1;}
    38                 }
    39                 if(num>t)
    40                 t=num;
    41             }
    42             printf("%d %d %d
    ",m,n,t+1);
    43         }
    44     }
    45     return 0;    
    46 }

    nyoj 271 各种TML ,各种优化,各种TML,AC代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int d[10003];
     5 
     6 int main()
     7 {
     8     int i,j,t,k;
     9     int max;
    10     int m,n;
    11     memset(d,0,sizeof(d));
    12     for(k=2;k<10003;k++)//打表
    13     {
    14         t=k;
    15         while(1!=t)
    16         {
    17             if(t&1)
    18             {
    19                 t=t*3+1;
    20             }
    21             else  t>>=1;
    22             d[k]++;
    23         }
    24     }
    25     while(scanf("%d %d",&m,&n)!=EOF)
    26     {
    27         max=t=0;
    28         i=m;j=n;
    29         if(i>j)
    30         {
    31             t=i;
    32             i=j;
    33             j=t;
    34         }
    35         for(k=i;k<=j;k++)
    36         {
    37           if(max<d[k])
    38             max=d[k];
    39         }
    40          printf("%d %d %d
    ",m,n,max+1);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    01 Java基础第一天
    2019牛客暑期多校训练营(第七场)J A+B problem
    SDNU 1477.矩形面积交(思维)
    SDNU 1194.传纸条(DP)&& 1032.机器人
    SDNU 1280.就问你慌不慌(高精度)
    POJ 2528 Mayor's posters(线段树+离散化)
    HDU 1698 Just a Hook(线段树区间赋值)
    POJ 3468 A Simple Problem with Integers (区间加区间查找)
    HDU 1754 I Hate It(线段树单点更改、区间查找最大值)
    HDU 1166 敌兵布阵(线段树单点加区间查询)
  • 原文地址:https://www.cnblogs.com/xl1027515989/p/3388799.html
Copyright © 2011-2022 走看看