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: 兩光法師占卜術 背景知识 ...
 
随机推荐
- nginx常用配置3
			
## 六.浏览器本地缓存配置 语法:expires 60 s|m|h|d ```动静分离效果: server { listen 80; server_name localhost; location ...
 - new Map的妙用
			
const actions = new Map([ [1, ['processing','IndexPage']], [2, ['fail','FailPage']], [3, ['fail','Fa ...
 - Kubeadm and Kops
			
Kubeadm是Kubernetes官方推出的快速部署Kubernetes的集群工具,其思路是将Kubernetes相关服务容器化以简化部署. With the release of kubeadm ...
 - Ajax(Asychronous JavaScript and XML)笔记
			
1 Ajax简介 1 ajax概念 2 什么是同步?什么是异步? 3 ajax原理 2 JavaScript原生的ajax 1 ajax.html代码 <!DOCTYPE html> &l ...
 - 九度oj 题目1009:二叉搜索树
			
题目1009:二叉搜索树 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5733 解决:2538 题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n&l ...
 - html控件
			
checkbox val = "<li class='layer'><label><input type='checkbox' checked name='la ...
 - wcf 基本配置
			
<system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...
 - SailingEase .NET Resources Tool (.NET 多语言资源编辑器)转
			
转自:http://www.cnblogs.com/sheng_chao/p/5958846.html 软件下载链接 痛点: 通常我们为了让软件支持多语言,会使用 .NET 自带的资源文件来存储不同的 ...
 - 利用canvas制作图片(可缩放和平移)+相框+文字
			
前言: 公司一个售前问我能不能用H5做一个手机拍照,给相片添加相框和添加文字上传到服务器的功能,我当时一琢磨觉得可行,就利用空余时间做了一个demo,去掉了拍照和上传,如果以后有机会,会给补上,当然对 ...
 - svn在commit后报错:is scheduled for addition, but is missing
			
今天通过svn 的cr(code review)代码审核后,我欲执行svn ci -m"xxxxxxx(提交注释) ISSUE=3380305",但是没有提交成功,SVN报错啦! ...