1964-NP
描述
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known 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 (including the 1). 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.
输入
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
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.
You can assume that no opperation overflows a 32-bit integer.
输出
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one 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).
样例输入
1 10
100 200
201 210
900 1000
样例输出
1 10 20
100 200 125
201 210 89
900 1000 174
#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
int np(int x)
{
int count=1;
while(x!=1)
{
count++;
if(x%2==0) x/=2;
else x=3*x+1;
}
return count;
}
int solve(int s, int e)
{
int res=0,i,tmp;
if(s>e)
swap(s,e);
for(i=s;i<=e;++i)
{
tmp=np(i);
if(res<tmp)
res=tmp;
}
return res;
}
int main()
{
int x,y,i;
while(cin>>x>>y)
{
cout<<x<<" "<<y<<" "<<solve(x,y)<<endl;
}
return 0;
}
1964-NP的更多相关文章
- 转载 什么是P问题、NP问题和NPC问题
原文地址http://www.matrix67.com/blog/archives/105 这或许是众多OIer最大的误区之一. 你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜 ...
- HDU1760 A New Tetris Game NP态
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- P与NP问题
Polynomial Nondeterministic Polynomial P问题: 一个问题可以在多项式时间复杂度内解决 NP问题: 一个问题可以在多项式时间内证实或者证伪 NP-Hard问题: ...
- 浅谈P NP NPC
P问题:多项式时间内可以找到解的问题,这个解可以在多项式时间内验证. NP问题:有多项式时间内可以验证的解的问题,而并不能保证可以在多项式时间内找到这个解. 比如汉密尔顿回路,如果找到,在多项式时间内 ...
- (数学)P、NP、NPC、NP hard问题
概念定义: P问题:能在多项式时间内解决的问题: NP问题:(Nondeterministic Polynomial time Problem)不能在多项式时间内解决或不确定能不能在多项式时间内解决, ...
- P,NP,NP_hard,NP_complete问题定义
背景:在看李航的<统计学习方法时>提到了NP完全问题,于是摆之. 问题解答:以下是让我豁然开朗的解答的摘抄: 最简单的解释:P:算起来很快的问题NP:算起来不一定快,但对于任何答案我们都可 ...
- P和NP问题
1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html NP----非定常多项式(英语:non-determin ...
- P,NP,NPC,NPC-HARD
P: 能在多项式时间内解决的问题 NP: 不能在多项式时间内解决或不确定能不能在多项式时间内解决,但能在多项式时间验证的问题 NPC: NP完全问题,所有NP问题在多项式时间内都能约化(Reducib ...
- NP完全问题 NP-Completeness
原创翻译加学习笔记,方便国人学习算法知识! 原文链接http://www.geeksforgeeks.org/np-completeness-set-1/ 我们已经找到很多很高效的算法来解决很难得问题 ...
- np问题
NP(np) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子 ...
随机推荐
- Swift泛型和泛型函数
泛型(generic)可以使我们在程序代码中定义一些可变的部分,在运行的时候指定.使用泛型可以最大限度地重用代码.保护类型的安全以及提高性能.在Swift集合类中,已经采用了泛型.一.一个问题的思考怎 ...
- JS判断移动设备最佳方法
最实用的还是下面这个: 方法一:纯JS判断 使用这方法既简单,又实用,不需要引入jQuery库,把以下代码加入到<head>里即可. <script type=”text/javas ...
- 让 Putty 保存密码,自动登陆的四种方法
Putty 基本是我在紧急时候用来登陆 Linux/Unix 终端的不二之先,因其小,开源,界面也非常实用.可是当你要在私有的机器上,经常性的要登陆很多机器的时候就觉得烦琐了,不光打开一堆的窗口,还要 ...
- 永远的月亮 2007? (献给L之二)
文/安然 您是我心中永远的月亮 已经走远在曾年少的梦想 但是,蒙胧而明亮的月光永远珍藏 夜夜升起般不忘…… 多年后的路上依旧会有迷茫 梦中,又一次回到您的课堂 感受您暴躁的激情和无言深情的期望 当又一 ...
- libevent 初试
一直就想用一下libevent库,直到今天才去码代码.用法在他的头文件里面写的很清楚,原理的话也不是很难,感谢作者做的工作! 今天做了几个探索: 实现自定义事件类型的设置与触发 尝试 setInter ...
- 最全 Adobe 系列产品 CS6版本 序列号/注册码
最全 Adobe 系列产品 CS6版本 序列号/注册码: 1.Adobe Photoshop CS6 Extended 序列号/注册码 2.Adobe After Effects CS6 序列号/注册 ...
- Linux的网卡由eth0变成了eth1,如何修复 (转载)
Linux的网卡由eth0变成了eth1,如何修复 使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间后,不知道为什么eth0无法使用,系统却自动生 ...
- sizeof() 之 数组
在平时的编程中,我们会经常用到数组,并且需要知道数组的长度,有时我们可以明确的知道数组的长度,但有时并不,这时,可以借用sizeof(),来获得数组的长度,如下: arrayLength = size ...
- sizeof的用法的一些归纳
1 sizeof 是运算符,不是函数 2 sizeof 不能求得void类型的长度,能求得 void*类型的指针的长度 sizeof(void) 会导致编译错误.因为声明一个变量的最重要的作用就是告诉 ...
- 初识shell脚本
shell字符串.shell数组.shell echo指令.shell test命令.shell if语句.shell case语句.shell for语句.shell while语句.shell b ...