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。
#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的更多相关文章
- UVa 694 - The Collatz Sequence
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...
- (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 ...
- projecteuler---->problem=14----Longest Collatz sequence
title: The following iterative sequence is defined for the set of positive integers: n n/2 (n is eve ...
- Project Euler 14 Longest Collatz sequence
题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...
- UVA_694:The Collatz Sequence
Language: C++ 4.8.2 #include<stdio.h> int main(void) { long long int m, n, copy_m; ; ; ) { sca ...
- Project Euler Problem 14-Longest Collatz sequence
记忆化搜索来一发.没想到中间会爆int #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; in ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 【索引】Volume 0. Getting Started
AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 0. Getting Started 10055 - Hashmat the Brav ...
- Zerojudge解题经验交流
题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...
随机推荐
- python fileinput处理多文件
import fileinput with fileinput.input(files=(path1,path2)) as f: for line in f: print(line)
- APP和小程序哪个更合适企业
很多企业都在纠结做APP好呢,还是做小程序好,那么我们来说说APP和小程序之间的区别,希望通过对比让您了解APP和小程序的功能,让您的企业在制作APP和小程序之间有一个选择. 一. 面向用户群 App ...
- (转)MySQL Proxy使用
转自: http://www.cnblogs.com/itech/archive/2011/09/22/2185365.html http://koda.iteye.com/blog/788862 h ...
- 关于display:none;和id特性的一些需要注意的地方
关注点一: display:none;一旦用于某个元素,那个这个元素在页面中就不再占据位置. visibility:hidden;用于某个元素时,这个元素还会占据位置. 关注点二: 即使使用了disp ...
- spring data jpa 动态查询(工具类封装)
利用JPA的Specification<T>接口和元模型就实现动态查询了.但是这样每一个需要动态查询的地方都需要写一个这样类似的findByConditions方法,小型项目还好,大型项目 ...
- orcale 之 初识数据库一
数据库 数据库顾名思义数据的仓库,只不过这个仓库是在计算机的存储设备之中.一般来说,这些数据面向一个组织,部门或者整个企业,这些数据是按照一定的模型进行存放的数据集合,比如对于一个学生的管理系统来说, ...
- Ubuntu下伪分布式模式Hadoop的安装及配置
1.Hadoop运行模式Hadoop有三种运行模式,分别如下:单机(非分布式)模式伪分布式(用不同进程模仿分布式运行中的各类节点)模式完全分布式模式注:前两种可以在单机运行,最后一种用于真实的集群环境 ...
- FocusBI: 总线矩阵(原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...
- weblogic JDBC Connection Pools--转官方文档
http://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html#1106016 JDBC C ...
- mysql 远程链接问题
问题描述: 从一台linux远程连接另一台linux上的MySQL, 出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.x ...