(HDU/UVA)1032/100--The 3n + 1 problem(3n+1问题)

描述
计算机科学中的问题通常被归类为属于某一类问题(例如,NP,不可解,递归)。在这个问题中,您将分析算法的属性,该算法的分类对于所有可能的输入都是未知的。

考虑下面的算法:

1.输入n

2.输出n

3.如果n = 1,则停止

4.如果n是奇数,则n=3n + 1

5.否则n=n / 2

6.返回第2步

给定输入22,将输出以下序列的数字22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

据推测,使用上述算法任何输入值将终止(当输出1时)。尽管算法简单,但是不知道这个猜想是否为真。

然而,已经验证了所有整数n,使得0 <n <1,000,000(并且实际上,比这更多的数目也成立)。

给定输入n,可以确定输出的数字的数量(包括1)。对于给定的n,这被称为n的周期长度。在上面的例子中,22的周期长度为16。

对于任何两个数字i和j,您将确定i和j之间的所有数字的最大循环长度。

输入
输入将由一系列整数对i和j组成,每行一对整数。所有整数将小于1,000,000和大于0。

你应该处理所有的整数对,并且对每个对确定i和j之间的所有整数的最大循环长度。

您可以假设没有操作溢出32位整数。

输出
对于每对输入整数i和j,应输出i,j和i和j之间的整数的最大循环长度。这三个数字应该由至少一个空格分隔,一行上的所有三个数字和每行输入的一行输出。整数i和j必须以与它们在输入中出现的顺序相同的顺序出现在输出中,并且后跟最大循环长度(在同一行上)。

样例输入
1 10
100 200
201 210
900 1000
样例输出
1 10 20
100 200 125
201 210 89
900 1000 174

#include <iostream>
#include <cstdio>

using namespace std;

int cnt(int n)
{
    int cnt=1;
    while(n!=1)
    {
        if(n%2==0) n=n/2;
        else n=3*n+1;
        cnt++;
    }
    return cnt;
}

int main()
{
    int i,j,t;
    while(~scanf("%d %d",&i,&j))
    {
        printf("%d %d ",i,j);
        if(i>j)
        {
            int temp;
            temp=i;
            i=j;
            j=temp;
        }
        int ans=0;
        for(t=i;t<=j;t++)
        {
            if (cnt(t)>ans) ans=cnt(t);
        }
        printf("%d\n",ans);
    }
    return 0;
}

题目有个坑哦

时间: 2024-09-30 16:28:59

(HDU/UVA)1032/100--The 3n + 1 problem(3n+1问题)的相关文章

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): 26353    Accepted Submission(s): 9784 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Comp

HDU 1032 The 3n + 1 problem【递归】

/* 中文题目 3n+1问题 中文翻译-大意 当一个数n是奇数的时候变成3*n+1,为偶数的时候变成n/2 解题思路:区间内每个数逐个求解 难点详解:如何逐个计算每个数的次数,选用while循环,还有就是将此时的 i 值赋给data,用于while循环的条件.最后再将这一个数运算次数累加在一起 关键点:理解题意 解题人:lingnichong 解题时间:2014-06-03 09:40:39 解题体会:没有告诉你那个大那个小,要先比较一下数,是个陷阱 */ The 3n + 1 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 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

HDU 1032.The 3n + 1 problem【注意细节】【估计数据不强】【8月21】

The 3n + 1 problem 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 classificat

HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】

The 3n + 1 problem 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 classificat

The 3n + 1 problem(杭电1032)(暴力求解)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23163    Accepted Submission(s): 8653 Problem Description Problems in Computer Science are often classified as belonging to a

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

POJ1207 The 3n + 1 problem

这题不是很难,模拟一下即可,但有一些细节需要注意 输入的数不一定升序,且要按原顺序输出,还有就是读完数据的问题 The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53006   Accepted: 16841 Description Problems in Computer Science are often classified as belonging to a certain cl

The 3n + 1 problem

The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50648   Accepted: 16059 Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In th