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: 兩光法師占卜術 背景知识 ...
随机推荐
- npm run build报错 ,resolve is not defined
今天在build项目的时候报: ReferenceError: resolve is not defined npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ...
- mojing手柄遥杆控制
using UnityEngine; using UnityEngine.UI; using System.Collections; using MojingSample.CrossPlatformI ...
- kafka producer自定义partitioner和consumer多线程
为了更好的实现负载均衡和消息的顺序性,Kafka Producer可以通过分发策略发送给指定的Partition.Kafka Java客户端有默认的Partitioner,平均的向目标topic的各个 ...
- 【随笔】关于绝对定位absolute相对于父元素定位的问题
绝对定位absolute的官方定义: 设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块.元素原先在正常文档流中所占的空间会关闭,就好像该元素 ...
- Linux下实现MySQL数据库自动备份
1.给mysql创建用户备份的角色,并且授予角色SELECT, RELOAD, SHOW DATABASES, LOCK TABLES等权限. mysql> create user 'backu ...
- jQuery插件的开发(一)
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- Python对日期进行格式化
Python对日期进行格式化 把当前时间输出为2017-04-07 19:00:00.进入python交互命令行输入: > import datetime > currtime = dat ...
- 【c++】常识易混提示
1. struct 和 class 唯一的区别:默认的成员保护级别和默认的派生保护级别不同(前者为public,后者为private). 2. int *p = new int[23]; de ...
- resteasy上传单个文件/多个文件到阿里云服务器
代码如下: ExcelServerController.java package com.xgt.controller; import com.xgt.bean.bs.ExcelBean; impor ...
- 【HTML基础】表格和表单
本次博客的主要内容如下: meta和link 表格 表单 meta和link meta meta的属性有两种:name和http-equiv. name属性主要用于描述网页内容,对应与网页内容. 1. ...