UVA_694:The Collatz Sequence

 Language: C++ 4.8.2

#include<stdio.h>
int main(void)
{
    long long int m, n, copy_m;
    int count = 1;
    int number = 0;
    while(1)
    {
        scanf("%lld%lld", &m, &n); // uva要求用%lld读入有符号长整型
        if(m < 0 && n < 0)
            break;
        count = 1;
        copy_m = m;
        while(m != 1)
        {
            if(m % 2 == 0)
                m = m / 2;
            else
            {
                m = 3*m + 1;
                if(m > n)
                    break;
            }
            count++;
        }
        number++;
        printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n", number, copy_m, n, count); // 注意输出格式,数据类型不统一的话,MingW下会出现莫名奇妙的错误。
    }

    return 0;
}
时间: 2024-10-13 00:24:57

UVA_694:The Collatz Sequence的相关文章

projecteuler----&gt;problem=14----Longest Collatz sequence

title: The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that thi

UVaOJ 694 - The Collatz Sequence

题目很简单,但是一开始却得到了Time Limit的结果,让人感到很诧异.仔细阅读发现,题目中有一个说明: Neither of these, A or L, is larger than 2,147,483,647 (the largest value that can be stored in a 32-bit signed integer). 所以应该是A溢出造成了程序“死循环”,最终导致超时. -----------------------------------------------

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

UVa 694 - The Collatz Sequence

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=635 题目:给出了如下算法: step 1:选择任意一个正整数A为序列的第一个数: step 2:如A=1或者A>L,则停止 step 3:如A为偶数,则A=A/2,执行step 2: step 4:如A为奇数,则A=3A+1,执行step 2: 现在给

UVa694 - The Collatz Sequence(模拟)

Step1: 任选一个正整数A作为这个数列的第一项. Step2: 如果A=1则停止. Step3: 如果A为偶数,则A=A/2然后重新回到Step2. Step4: 如果A为奇数,则A=3*A+1然后重新回到Step2. 这个演算法已经被证明当首项小于等于109时这个数列最终都会在Step2停止,但是有些A值在这个数列中会超出许多电脑的整数上限.在这个问题中我们想??要计算这个数列的长度,而数列的终止有两种情况:1.最终会在Step2停止或是2.某一项会在Step4超出一个特定的上限. Inp

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

Project Euler 14 solution

Question: Longest Collatz sequence Problem 14 The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even)n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 4

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule