The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 53927 | Accepted: 17142 |
Description
for all possible inputs.
Consider the following algorithm:
1. input n 2. print n 3. if n = 1 then STOP 4. if n is odd then n <-- 3n+1 5. else n <-- n/2 6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that
0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed before the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
Input
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
Output
line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
#include <cstdio>
#include <string.h>
#include <cmath>
#include <iostream>
#define WW freopen("output.txt","w",stdout)
using namespace std;
const int Max=11000;
int Arr[Max];
int main()
{
memset(Arr,0,sizeof(Arr));
Arr[1]=1;
Arr[2]=2;
for(int i=3; i<Max; i++)
{
int ans=i;
int sum=1;
while(ans!=1)
{
if(ans<i)
{
break;
}
if(ans%2)
{
ans=ans*3+1;
}
else
{
ans/=2;
}
++sum;
}
Arr[i]=Arr[ans]+sum-1;
}
int Star,End;
int vis;
while(~scanf("%d %d",&Star,&End))
{
vis=0;
if(Star>End)//可能输入的开始比结束大,所以要交换
{
swap(Star,End);
vis=1;
}
int MAX=0;
int flag=0;
for(int i=Star; i<=End; i++)
{
if(MAX<Arr[i])
{
MAX=Arr[i];
flag=i;
}
}
if(!vis)//输出时要按照输入的顺序
printf("%d %d %d\n",Star,End,Arr[flag]);
else
{
printf("%d %d %d\n",End,Star,Arr[flag]);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏的更多相关文章
- Ubiquitous Religions 分类: POJ 2015-06-16 17:13 11人阅读 评论(0) 收藏
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 26678 Accepted: ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏
(一) strace 命令 用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
- 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏
RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...
- max_flow(Ford-Fulkerson) 分类: ACM TYPE 2014-09-02 01:50 110人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include<queue> usi ...
- JAVA 关于Icon,Image,ImageIcon的简单的对比参考 分类: Java Game 2014-08-14 17:08 210人阅读 评论(0) 收藏
转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...
- c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏
使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...
随机推荐
- js兼容
//document.getElementsByClassName(); if (!document.getElementsByClassName) { document.getElementsByC ...
- JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面
将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...
- CSS_03_01_CSS组合选择器
CSS组合选择器 第01步:创建css:with.css @charset "utf-8"; /* 组合选择器,用","隔开 */ .a,.b,div span ...
- -05 08:57 ARCGIS地统计学计算文件后缀名为.shp文件制作
2011-07-05 08:57 ARCGIS地统计学计算文件后缀名为.shp文件制作 ARCAMP软件要进行地统计计算的文件后格式后缀名必须为.shp的文件,网上介绍的方法复杂难懂,那么制作.shp ...
- paper 23 :Kullback–Leibler divergence KL散度(2)
Kullback–Leibler divergence KL散度 In probability theory and information theory, the Kullback–Leibler ...
- app_field.clear_dependent_fields
可以调用APP_FIELD.clear_dependent_fields和APP_FIELD.set_dependent_field来将两个(或多个)Item建立关联,当一个为空时,另一个不可录入,反 ...
- Sqlserver常用的时间函数---GETDATE、GETUTCDATE、DATENAME
GETDATE 按 datetime 值的 Microsoft® SQL Server™ 标准内部格式返回当前系统日期和时间.语法GETDATE ( )返回类型datetime注释日期函数可用在 SE ...
- Hadoop之TaskInputOutputContext类
在MapReduce过程中,每一个Job都会被分成若干个task,然后再进行处理.那么Hadoop是怎么将Job分成若干个task,并对其进行跟踪处理的呢?今天我们来看一个*Context类——Tas ...
- 如何把一个java工程打成一个jar包(转载)
1.jar包有入口(即有main()函数) 选中要打包的工程--->右键选择Export---->java----->Runnable java file----->next- ...
- SqlServer中使用Select语句给变量赋值的时候需要注意的一个问题
我们知道在SqlServer中可以用Select语句给变量赋值,比如如下语句就为int类型的变量@id赋值 ; select @id=id from ( as id union all as id u ...