UVA 100 The 3n + 1 problem(超级大水题)

The 3n + 1 problem

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

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 

5. else 

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-lengthof 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 iand j.

You can assume that no operation overflows a 32-bit integer.

Output

For each pair of input integers i and j you should output ij, 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

题目大意:

输出i到j中循环次数最多的。

解题思路:

唯一的坑点是i,j输入的大小关系是不确定的。

代码:

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int maxn=0;
        printf("%d %d ",n,m);
        if(n>m){
            int temp=n;n=m;m=temp;
        }
        for(int i=n;i<=m;i++){
            int j=i,cnt=1;
            while(j!=1){
                if(j%2==0){
                    j=j/2;
                    cnt++;
                }
                else {
                    j=3*j+1;
                    cnt++;
                }
            }
            if(cnt>maxn){
                maxn=cnt;
            }
        }
    printf("%d\n",maxn);
    }
    return 0;
}

UVA 100 The 3n + 1 problem(超级大水题)

时间: 2024-08-08 20:31:42

UVA 100 The 3n + 1 problem(超级大水题)的相关文章

uva 100 - The 3n + 1 problem

1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 void print(int i, int j) 6 { 7 if(i > j) { int temp = i; i = j; j = temp; } 8 9 int max = 0; 10 for(int k = i; k <= j; k++) 11 { 12 int n = k, sum = 1; 13 while(n != 1)

UVA12709 Falling Ants(超级大水题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4447 Ps:此题超级水,但是光看题目就会被吓一跳.确实题目很长,不过有用的语句少之又少,介绍一大堆与AC没有半毛钱关系的东西,汗颜. 此题告诉我们题目长不一定是最难得题,反而有时候是最简单的题,就看你有

100 - The 3n + 1 problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36  The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a certain class of problem

HDU 1032 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): 22148    Accepted Submission(s): 8276 Problem Description Problems in Computer Science are often classified as belonging to a c

UVA 11389 The Bus Driver Problem 贪心水题

题目链接:UVA - 11389 题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机.如果一个司机早班和晚班总的驾驶时间超过d,那么超出的时间按每小时r元付给司机.求最小的费用. 算法分析:一枚贪心的小水题.对早班路线的时间按照从大到小排序,对晚班路线的时间按照从小到大排序,然后就可以了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cs

codeforces 892A - Greed - [超级大水题][O(n)数组最大和次大]

题目链接:https://cn.vjudge.net/problem/CodeForces-892A Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ?≤? bi). Jafar has decided to pour all remaining cola into just 2 cans, determin

hdu 1229 超级大水题

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1. Input 测试输入包含若干测试用例,每个测试用例占一行,格式为"A B K",相邻两数字有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.

BNU 斩(超级大水题)

L. 斩 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 29379 在电影里面,我们经常可以看到武士们拔出刀,然后一刀斩下去,结果------------一张纸片被砍成了两半,囧---- 而在本题中,我们需要计算一下被斩下去较小的那一部分的面积. 我们假设纸片是矩形的,平行于坐标轴的,武士砍纸片的轨迹是一条直

HDU The 3n + 1 problem(简单题 有坑)

最大的坑就在输入的两个变量的大小并没有规定前面大于后面... 1 #include <stdio.h> 2 3 //普通遍历 4 int Program(int n) 5 { 6 int num = 0; 7 while (1) 8 { 9 num++; 10 if (n == 1) 11 { 12 break; 13 } 14 else if (n % 2 != 0) 15 { 16 n = 3 * n + 1; 17 } 18 else 19 { 20 n /= 2; 21 } 22 }