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溢出造成了程序“死循环”,最终导致超时。

-------------------------------------------------------------

将A由int改为long long后即正确。其中cin,cout默认支持long long。

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     long long a, lim,a_store;
 6     int count;
 7     int casenum=0;
 8     //cin >> a>>lim;
 9     //while (a>=0||lim>=0)
10     while (cin >> a >> lim)
11     {
12         casenum++;
13         a_store = a;
14         count = 1;
15         if (a < 0 && lim < 0)
16             return 0;
17         while (a != 1 && a <= lim)
18         {
19             if (a % 2)
20             {
21                 a = a * 3 + 1;
22             }
23             else
24             {
25                 a = a / 2;
26             }
27             count++;
28         }
29         if (a > lim)
30             count--;
31
32         cout <<"Case "<< casenum<<": A = " << a_store << ", limit = " << lim << ", number of terms = " << count<<endl;
33     }
34     return 0;
35 }

UVaOJ 694 - The Collatz Sequence

时间: 2024-10-13 17:30:12

UVaOJ 694 - The Collatz Sequence的相关文章

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: 现在给

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

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

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

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

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 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,它们之和是

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