UVA 100 The 3*n+1 problem

 

UVA 100 The 3*n+1 problem.

解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+1)进过操作至1的最大步数。

对m到n依次计算,比较获得最大步数。

注意:1.输入格式。

本题利用  while(scanf("%d %d",&m,&n)!=EOF) 

                {

                }

      2.输出与题给出相同。

3。对当边界为1<n<1 000 000时变量类型的选取。

代码如下:

#include <stdio.h>
#include <stdlib.h>

void FindResult(int Min,int Max)
{
    int i,count,temp;
    temp=0;       //temp存放最大步数
    long long n;  //注意这n类型为long long型,当这为int时,计算837799溢出
    for(i=Min;i<=Max;i++)
    {
        n=i;
        count=0;
        while(1)
        {
            if(n==1)
            {
                count++;
                if(temp<count)
                {
                    temp=count;
                }
                break;
            }
            if(n%2==1)
            {
                n=3*n+1;
                count++;
            }
            else
            {
                n=n/2;
                count++;
            }
        }

    }

    printf("%d\n",temp);
}

int main()
{
    int MaxN,MinN,t;       //MinN下界,MaxN为上界。
    t=0;
    while(scanf("%d %d",&MinN,&MaxN)!=EOF)
    {
        printf("%d %d ",MinN,MaxN);
        if(MinN>MaxN)
        {
            t=MinN;
            MinN=MaxN;
            MaxN=t;

        }
        FindResult(MinN,MaxN);   //计算最大步数

    }
    return 0;
}

以上代码不足之处请指出,谢谢

如果有更好的方法,望不吝赐教,大家相互学习进步。

时间: 2024-08-29 09:03:34

UVA 100 The 3*n+1 problem的相关文章

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

The 3n + 1 problem UVA - 100

3n+1问题 PC/UVa IDs: 110101/100 Popularity: A Success rate: low Level: 1 测试地址: https://vjudge.net/problem/UVA-100 [问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该算法生成的序列是: 22,11,34,17,52,26,13,40,20,1

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)

UVA - 10239 The Book-shelver&amp;#39;s Problem

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t

UVA 100 3n+1问题 (扑街题)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36 叼伱个扑街,对呢道题真系无语,,提交5次都过唔到,唔相信既话,伱地可以试下啦... 偶噶代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdli

UVa 11389 (贪心) The Bus Driver Problem

题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感觉上是要先排序,然后时间最长的路线配另一个时间最短的路线. 这里就严格证明一下这样贪心的正确性. 以两条路线为例,其他情况都是类似的: 不妨假设:A1≥A2,B1≤B2,水平线代表d 情况一: 如图,司机一要付加班费,司机二不用,如果我们将B1.B2交换: 因为B1≤B2,所以付给司机一的加班费不会

(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时).尽管算法简单,但是不知道

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

uva 11205 The broken pedometer (暴力)

uva 11205 The broken pedometer The Problem A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs): But the pedometer does not work properly (possibly the sweat aff