UVa 694 - The Collatz Sequence
题目:给出了如下算法;
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;
现在给定初始值A,和限定最大数值L;求从A开始,到A=1||A>L,经过数值个数。
思路: 当A为偶数时,A值减小,如A减小到等于1,则停止。如A为奇数,则A增大,当A>L则停止。
#include<iostream>
#include<cstdio>
using namespace std; int main()
{
//freopen("input.txt","r",stdin);
int a1,b1,count,k=;
long long a,b,c;
while(cin>>a1>>b1)
{
k++;
if(a1<=&&b1<=)
break;
count=;
a=a1;b=b1;
while(a%==)
{
a=a>>;
count++;
}
while()
{
if(a==)
break;
if(a%)
{
c=*a+;
if(c<=b)
{
a=c;count++;
while(a%==)
{ a=a>>;
count++;
}
}
else
break;
}
}
cout<<"Case "<<k<<": A = "<<a1<<", limit = "<<b1<<", number of terms = "<<count<<endl;
}
return ;
}
UVa 694 - The Collatz Sequence的更多相关文章
- UVaOJ 694 - The Collatz Sequence
题目很简单,但是一开始却得到了Time Limit的结果,让人感到很诧异.仔细阅读发现,题目中有一个说明: Neither of these, A or L, is larger than 2,147 ...
- UVA 1546 - Complete the sequence!(差分法)
UVA 1546 - Complete the sequence! 题目链接 题意:给定多项式前s项,求出后c项,要求尽量小 思路:利用差分法,对原序列求s - 1次差分,就能够发现规律,然后对于每多 ...
- UVA 10534 三 Wavio Sequence
Wavio Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- (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 ...
- UVA - 348Optimal Array Multiplication Sequence(递推)
id=19208">题目:Optimal Array Multiplication Sequence 题目大意:给出N个矩阵相乘.求这些矩阵相乘乘法次数最少的顺序. 解题思路:矩阵相乘 ...
- UVA_694:The Collatz Sequence
Language: C++ 4.8.2 #include<stdio.h> int main(void) { long long int m, n, copy_m; ; ; ) { sca ...
- UVa 1626 (输出方案) Brackets sequence
正规括号序列定义为: 空序列是正规括号序列 如果S是正规括号序列,那么[S]和(S)也是正规括号序列 如果A和B都是正规括号序列,则AB也是正规括号序列 输入一个括号序列,添加尽量少的括号使之成为正规 ...
- UVA 10479 The Hendrie Sequence
https://vjudge.net/problem/UVA-10479 打表找规律: 1.根据n可以确定第n项在上表中第i行 2.减去前i-1行,就得到了n在第i行的第j个 3.第i行的规律:1个i ...
随机推荐
- 【转】Java代码规范
[转]Java代码规范 http://blog.csdn.net/huaishu/article/details/26725539
- HTML input="file" 浏览时只显示指定文件类型 xls、xlsx、csv
html input="file" 浏览时只显示指定文件类型 xls.xlsx.csv <input id="fileSelect" type=" ...
- linux下解压大于4G文件提示error: Zip file too big错误的解决办法
error: Zip file too big (greater than 4294959102 bytes)错误解决办法.zip文件夹大于4GB,在centos下无法正常unzip,需要使用第三方工 ...
- IOS ScrollView放大缩小点击位置并居中
项目中的一个优化案例,提升用户体验,对地铁线路图点击放大.缩小,并且点击位置居中: 正常ScrollView 我们点击某一点比如屏幕右侧,想要点的位置向左移动到中心位置,很简单只有算出该点位置距中心位 ...
- SQL2008 清除日志
USE [master] GO ALTER DATABASE BizTest SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE BizTest SE ...
- Sql Server 2008和2000查询表的字段和注释
-- SQL Server 2008 SELECT 表名 = d.name, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' ...
- Business Unit Lookup in Form
Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...
- 【java】分页查询实体类
package com.dmsd.itoo.tool.pageModel; import java.io.Serializable; import java.util.HashMap; import ...
- Delphi系统变量:IsMultiThread对MM的影响
前几日,调试一BUG,过程先不说,最后调试到MM,即Debug dcu,然后进入到GetMem.inc中的Get/FreeMem函数处后,出现AV. 然后一通找...郁闷了N天,后来发现将MM切换到Q ...
- QT不同版本编译
QT发布了不同版本,有一些语法修改,需要修改代码.同时旧版本代码转换需要在pro文件中添加代码greaterThan(QT_MAJOR_VERSION, 4): QT += widgets