YT14-HDU-求范围内多个数经过变换后的那个最大值

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

代码如下:

#include <iostream>
using namespace std;
int main()
{
    int i,j,t;
    int n,a,b,k,max;
    while (cin>>i>>j)
    {
        a=i;
        b=j;
        if (i>j)     //如果i>j,使得i和j调换位置,方便后续计算
        {
            t=i;
            i=j;
            j=t;
            a=j;
            b=i;      //如果a,b发生变换,则后面输出的时候需要注意
        }
        max=0;
        for (;i<=j;i++)
        {
            k=1;
            n=i;
            do
            {
                if (n==1)
                    break;
                if (n%2==1)
                    n=3*n+1;
                else
                    n/=2;
                k++;
            }while (1);
            if (max<k)
                max=k;
        }
        cout<<a<<" "<<b<<" "<<max<<endl;
    }
    return 0;
}

解题思路:

题目要求很容易读懂,给定一个范围,在这个范围内的数经过一系列的变换后输出范围区间和区间内的最大值

关键是给定范围时的两个数孰大孰小,当输出的时候也要和原来保持一样的顺序



时间: 2024-11-04 20:50:47

YT14-HDU-求范围内多个数经过变换后的那个最大值的相关文章

递归求文件内文件个数 java

递归求取文件夹内文件的个数.判断文件是否是目录,如果是继续遍历,如果不是则求文件个数. public static long getlist(File f){//递归求取目录文件个数 long size = 0; File flist[] = f.listFiles(); size=flist.length; for (int i = 0; i < flist.length; i++) { if (flist[i].isDirectory()) { size = size + getlist(f

HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5101    Accepted Submission(s): 2339 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping abilit

hdu 4353 统计点在三角形内的个数

Finding Mine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1120    Accepted Submission(s): 298 Problem Description In bitland, there is an area with M gold mines. As a businessman, Bob wants t

HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)

Problem Description Now you are back,and have a task to do: Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s. And you have some query,each time you should calculate f(s[l...r]), s[l

求100内质数的个数

// 求100内质数的个数 非容斥 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> using namespace std; int n,sum; int main() { scanf("%d",&n); for(int i=1;i<=

hdu3709(求区间内平衡数的个数)数位dp

题意:题中平衡数的定义: 以一个位置作为平衡轴,然后左右其他数字本身大小作为重量,到平衡轴的距离作为全职,实现左右平衡(即杠杆原理平衡).然后为区间[x,y]内平衡数的个数. (0 ≤ x ≤ y ≤ 1018) 解法:数位dp.如果一个数的平衡数,那么它的平衡轴位置是确定的.原来一直尝试数位dp在dfs时候列举平衡轴的位置,后来才意识到可以提前枚举平衡轴位置,然后再dfs,这样比较好写.dp[mid][pre][wei];表示对称轴是mid,计算第pre个位置以后需要力矩大小wei的数的个数.

HDU - 3644:A Chocolate Manufacturer&#39;s Problem(模拟退火, 求多边形内最大圆半径)

pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去.  问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那么简单.  因为我们的多边形可能是凹多边形,而前面的方法只对凸多边形有效. 学习了下模拟退火的算法,这个随机算法只在最小圆覆盖的时候写过. 这里再学一下,看起来更正宗一点的.  每次在当前点的附近(R)找是否能优化,而这个R慢慢变小,使得趋紧答案的趋势更精细. 判定点再多边形内:同样,不能用检验是否在每条

求1-100内的素数(包括100)

public class PrimeNumber { // 求1-100内的素数(包括100) public static void main(String[] args) { int j = 0; System.out.println("输出1-100之间的素数"); for (int i = 2; i <= 100; i++) {// 1既不是苏数,也不是合数 if (i < 4) { System.out.println(i); continue;//注意此处有con

hdu 5869 区间不同GCD个数(树状数组)

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 58 Problem Description This is a simple problem. The teacher gives Bob a list of probl