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 <cstdlib>
#define MAXN 10005
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

int main()
{
    int n, m, Ms, Me;
    while(~scanf("%d%d", &n, &m)) {
        int res = 0;
        Ms = n, Me = m;
        if(n > m) { Ms = m, Me = n; }
        for(int i=Ms; i<=Me; i++) {
            int Mc = i, cnt = 1;
            while(Mc != 1) {
                if(Mc%2 == 1) Mc = Mc*3+1;
                else Mc /= 2;
                cnt++;
            }
            if(cnt > res) res = cnt;
        }
        printf("%d %d %d\n", n, m, res);
    }
    return 0;
}
时间: 2024-08-01 14:00:56

UVA 100 3n+1问题 (扑街题)的相关文章

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

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.输出与题给出相同

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 10200 Prime Time 暴力水题

一个简单的暴力水题,只是输出方式让人无语... #include <stdio.h> #include <string.h> int prime(int n) { int i; for(i=2;i*i<=n;i++) { if((n%i)==0) return 0; } return 1; } int main() { int num[10010]; int i; int a,b; int sum; memset(num,0,sizeof(num)); for(i=0;i&l

UVa 725 - Division(枚举简单题)

今日无事,写篇日记.这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间. 题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果. 1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了. 2.复杂度:枚举分子然后分子乘

UVA 11947 Cancer or Scorpio 水题

Cancer or Scorpio Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3098 Description Alice and Michael is a young couple who are planning on having their

UVa - The 3n + 1 problem 解读

这个问题并计算质数了一下相间隔似的.思想上一致. 注意问题: 1 i 可能 大于或等于j -- 这里上传.小心阅读题意,我没有说这个地方不能保证.需要特殊处理 2 计算过程中可能溢出,的整数大于最大值,需要使用long long 关于效率和时间问题: 1 能够使用数组保存中间结果,这样执行快了.内存消耗大了, 2 能够不使用中间数组. 这样执行肯定比較慢了.可是内存消耗小,一样能够AC的. 全部没有个标准吧.看情况而定.假设须要速度就添加数组,假设省内存就不使用数组吧. 这样的题目一般都使用数组

100个苹果(智力题四)

问题: 桌上有100个苹果,你和另一个人一起拿,一人一次,每次拿的数量大于等于1小于等于5,问:如何拿能保证最后一个苹果由你来拿? 解答:只需要你先拿,第一次拿4个,以后看对方拿的个数,根据对方拿的个数,保证每轮对方和你拿的加起来是6就行了,其实就是保证你拿到4,还要拿到10,16-直到94. 分析:如果要保证拿最后一个,那么就得保证拿到第94个,以此类推,要拿第94个,就要保证拿到第88个.82.76.70-最后只要保证你拿到第四个就行了. 版权声明:本文为博主原创文章,未经博主允许不得转载.