100 - The 3n+1 problem (3n+1 问题)

/*
* 100 - The 3n+1 problem (3n+1 问题)
* 作者 仪冰
* QQ 974817955
*
* [问题描述]
* 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2;如果 n 是奇数,
* 把它乘 3 加1。用新得到的值重复上述步骤,直到 n = 1 时停止。
* 例如,n = 22 时该算法生成的序列是:
* 22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
* 人们猜想(没有得到证明)对于任意整数 n,该算法总能终止于 n = 1。
* 这个猜想对于至少 1 000 000 内的整数都是正确的。
* 对于给定的 n,该序列的元素(包括 1)个数被称为 n 的循环节长度。
* 在上述例子中,22 的循环节长度为 16。
* 输入两个数 i 和 j,你的任务是计算 i 到 j(包含 i 和 j)之间的整数中,
* 循环节长度的最大值。 * [输入]
* 输入每行包含两个整数 i 和 j。所有整数大于 0,小于 1 000 000。
*
* [输出]
* 对于每对整数 i 和 j,按原来的顺序输出 i 和 j,然后输出二者之间的整数中的最大循环节长度。
* 这三个整数应该用单个空格隔开,且在同一行输出。
* 对于读入的每一组数据,在输出中应位于单独的一行。
*
[样例输入]
1 10
100 200
201 210
900 1000 [样例输出]
1 10 20
100 200 125
201 200 27
900 1000 174 * [解题方法]
* 计算每个数的循环节长度,求给定区间的最大值。
*
* 需要注意:
* 1.中间计算过程有的数据会超出int 或 long 型范围,应该用long long型。
* 2.输入的两个数,应该比较大小,判断出左区间和右区间。
* 3.直接按部就班的计算会time limited(超时),这里采用填表的方法,
* 把算出来的结果保存在一个全局数组中,这样以后用到的时候,直接拿来用,节省时间,避免超时。
*/ #include<iostream>
#include<cstring> using namespace std; const int MAXN = 1000000; //右端点最大值 int MediaVariableArray[MAXN]; //保存区间中所有的循环节长度,这是对填表的应用 int LoopNodeLength (long long EveryNumber); //计算循环节长度 int main()
{
int firstnumber = 0; //输入的第一个数
int secondnumber = 0; //输入的第二个数
int nodeleft = 0; //区间左端点
int noderight = 0; //区间右端点
int loopnodemax = 0; //保存最大长度的循环节
int everynodelength = 0; //保存区间中单个数的循环节长度 memset(MediaVariableArray, 0, sizeof(MediaVariableArray)); //初始化,都置为0 while (cin >> firstnumber >> secondnumber)
{
if (firstnumber > secondnumber) //判断左端点和右端点
{
nodeleft = secondnumber;
noderight = firstnumber;
}
else
{
nodeleft = firstnumber;
noderight = secondnumber;
} loopnodemax = 0; //初始化最大值
for (int i=nodeleft; i<=noderight; i++)
{
everynodelength = LoopNodeLength(i); if (everynodelength > loopnodemax) //判断是否更新最大值
{
loopnodemax = everynodelength;
}
} cout << firstnumber << " " <<secondnumber << " ";
cout << loopnodemax << endl;
} return 0;
} int LoopNodeLength(long long EveryNumber)
{
if (EveryNumber == 1)
{
return 1;
} if (EveryNumber & 1) //按位与1 等价于 对2求模;左移 等价于 乘以2,同理右移是除以2。
{
//计算n = 3n + 1;
//左移运算符比加号优先级低,所以加括号;
//我建议不管怎样,为了代码易读都应该加上必要的括号。
EveryNumber = EveryNumber + (EveryNumber<<1) + 1;
}
else
{
EveryNumber >>= 1; //n = n / 2
} //如果中间计算值小于MAXN就看看我们先前求没求出它的循环节长度,
//如果求出来了,直接拿来用,节省时间。
if (EveryNumber < MAXN)
{
if (MediaVariableArray[EveryNumber] == 0)
{
MediaVariableArray[EveryNumber] = LoopNodeLength(EveryNumber);
} return MediaVariableArray[EveryNumber] + 1;
} return LoopNodeLength(EveryNumber) + 1;
}

UVA 100 - The 3n+1 problem (3n+1 问题)的更多相关文章

  1. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  3. PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)

     The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a ...

  4. 100-The 3n + 1 problem

    本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...

  5. OpenJudge/Poj 1207 The 3n + 1 problem

    1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...

  6. The 3n + 1 problem

    The 3n + 1 problem Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  7. poj1207 3n+1 problem

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 60496   Accepted: 19 ...

  8. 杭电OJ——1032 The 3n + 1 problem

    The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...

  9. HDU 1032 The 3n + 1 problem (这个题必须写博客)

    The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 【HDU1875】畅通工程再续(MST基础题)

    更改成实形数即可.第一次敲完直接交,CE了一次.晕. #include <iostream> #include <cstring> #include <cstdio> ...

  2. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

  3. Python网络爬虫(6)--爬取淘宝模特图片

    经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...

  4. ASP.NET MVC:自定义 Route 生成小写 Url(转)

    先给出本文中测试用的 controller: public class PersonsController : Controller { public ActionResult Query(strin ...

  5. C#鼠标键盘钩子

    using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...

  6. commit后数据库干的工作

    用户提交commit后,数据库干的工作有: 1,oracle为用户的transaction生成一个SCN号. 2,LGWR把redo buffer中的数据写入到redo log file,同时把SCN ...

  7. wince下写入数据到csv/txt文件中

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用

                                             Default.aspx 网页界面 <%@ Page Language="C#" AutoE ...

  9. 关于Struts2的类型转换详解

    详细出处参考:http://www.jb51.net/article/35465.htm 一.类型转换的意义 对于一个智能的MVC框架而言,不可避免的需要实现类型转换.因为B/S(浏览器/服务器)结构 ...

  10. java集合--Queue用法

    队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...