题目很简单,但是一开始却得到了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。

 #include <iostream>
using namespace std;
int main()
{
long long a, lim,a_store;
int count;
int casenum=;
//cin >> a>>lim;
//while (a>=0||lim>=0)
while (cin >> a >> lim)
{
casenum++;
a_store = a;
count = ;
if (a < && lim < )
return ;
while (a != && a <= lim)
{
if (a % )
{
a = a * + ;
}
else
{
a = a / ;
}
count++;
}
if (a > lim)
count--; cout <<"Case "<< casenum<<": A = " << a_store << ", limit = " << lim << ", number of terms = " << count<<endl;
}
return ;
}

UVaOJ 694 - The Collatz Sequence的更多相关文章

  1. UVa 694 - The Collatz Sequence

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  2. (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 ...

  3. 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 eve ...

  4. Project Euler 14 Longest Collatz sequence

    题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...

  5. UVA_694:The Collatz Sequence

    Language: C++ 4.8.2 #include<stdio.h> int main(void) { long long int m, n, copy_m; ; ; ) { sca ...

  6. Project Euler Problem 14-Longest Collatz sequence

    记忆化搜索来一发.没想到中间会爆int #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; in ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. 【索引】Volume 0. Getting Started

    AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 0. Getting Started 10055 - Hashmat the Brav ...

  9. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

随机推荐

  1. hive Tutorial

    hive数据单元按照粒度从大到小,依次为 1.数据库database:可以用show databases; 命令查看所有的数据库,并用use d1; 命令来选中d1数据库,接下来就可以操作d1数据库中 ...

  2. Binder Native 层(二)

    Binder 框架及 Native 层 Binder机制使本地对象可以像操作当前对象一样调用远程对象,可以使不同的进程间互相通信.Binder 使用 Client/Server 架构,客户端通过服务端 ...

  3. 如何使一个div能够铺满整个页面? && 模态框的制作 && outerHTML

    说明: 使用 ele.outerHTML = '' 这样的方法可以很容易的清除一个元素. 当然也可以使用parent.removeChild() ,但是相较而言,还是使用 outerHTML = '' ...

  4. MySQL表级约束和列级约束

    对一个数据列建立的约束,称为列级约束 对多个数据列建立的约束,称为表级约束 列级约束即可以在列定义时生命,也可以在列定义后声明. 表级约束只能在列定义后声明. NOT NULL和DEFAULT只存在列 ...

  5. PHP如何判断对象为空的方法分享

    1.网上很多方法分享说用empty()方法判断对象是否为空.empty()的方法介绍:格式:bool empty ( mixed var )功能:检查一个变量是否为空返回值:若变量不存在则返回 TRU ...

  6. leetcode简单题目两道(1)

    Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...

  7. C#操作Redis SortedSet 有序集合

    /// <summary> /// Redis 有序集合 /// </summary> public static void Redis_SetSorted() { Redis ...

  8. JavaScript使用Object.defineProperty方法实现双数据绑定

    Object.defineProperty这个方法非常值得学习,很多mvc框架中的双向数据绑定就是通过它来实现的. 本着互联网分享精神,今天我就将我自己的见解分享给大家,希望能有所帮助. 开始使用 O ...

  9. window.open以post方式提交(转)

    function openWindowWithPost(url,name,keys,values) { var newWindow = window.open(url, name); if (!new ...

  10. DataTables添加额外的查询参数和删除columns等无用参数

    //1.定义全局变量 var iStart = 0, searchParams={}; //2.配置datatable的ajax配置项 "ajax": { "url&qu ...