uva----(100)The 3n + 1 problem
| The 3n + 1 problem |
Background
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.
The Problem
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then 
5. else 
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
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 operation overflows a 32-bit integer.
The Output
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).
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
给出一组数据i,j问在[i,j]中循环长度最大值;
避免重复运算,采用一个数组避免重复...
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
bool vis[maxn];
int s,t;
int main()
{
int cnt,ans,n;
bool flag=;
while(scanf("%d%d",&s,&t)!=EOF){
flag=;
if(s>t){
s^=t;
t^=s;
s^=t;
flag=;
}
memset(vis+s,,sizeof(bool)*(t-s+));
ans=-;
for(int i=s;i<=t;i++)
{
if(!vis[i]){
cnt=;
n=i;
while(n!=){
if(n&) n=*n+;
else n>>=;
cnt++;
if(n>=s&&n<=t) vis[n]=;
}
if(ans<cnt)ans=cnt;
}
}
if(flag)printf("%d %d %d\n",t,s,ans);
else printf("%d %d %d\n",s,t,ans);
}
return ;
}
uva----(100)The 3n + 1 problem的更多相关文章
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- uva 100 The 3n + 1 problem (RMQ)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...
- 【转】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 / ...
- 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 ...
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
参考:https://blog.csdn.net/metaphysis/article/details/6431937 #include <iostream> #include <c ...
- UVA 100 The 3*n+1 problem
UVA 100 The 3*n+1 problem. 解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+ ...
- 100-The 3n + 1 problem
本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- The 3n + 1 problem
The 3n + 1 problem Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
- SignalR记录
服务端检索数据库,有跟新,推送给客户端 1: GlobalHost.ConnectionManager.GetHubContext<tvHub>().Clients.Client(Clie ...
- SDP学习笔记
一.SDP规范了回话描述的格式,一般结合会话协议共同工作. 常见的会话传送协议包括:SAP(Session Announcement Protocol 会话公告协议),SIP,RTSP,HTTP,和使 ...
- R语言保存文件 Error in save error writing to connection
Error in save(filtered, file = paste(sampleName, "filtered", sep = "_")) : err ...
- 重命名myclipse中web项目名称的过程
1 打开 myclipse2 最顶层项目上右键,Refactor,新名,3 最顶层项目上右键,propterties, MyEclipse, Web, context-root: /新名4 替换 .c ...
- 从xubuntu-->windows xp
捣鼓了两个月的ubuntu之后我又乖乖的回到了windows的怀抱,不是抛弃linux而是要适应身边的环境. 身边的板子的驱动基本上都是xp的老一点的还是vista的,让人情何以堪. 我努力克服了,用 ...
- 转载java源代码阅读方法
刚才在论坛不经意间,看到有关源码阅读的),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧,如果你从来没有学过Java,或是任何一门编程语言 ...
- phpcms标签大全V9
转自:http://blog.csdn.net/cloudday/article/details/7343448调用头部 尾部 {template "content"," ...
- Nginx模块学习之————accesskey权限模块使用(Nginx防盗链详细解说),防止别人下载文件和图片
nginx 的第三方模块ngx_http_accesskey_module 来实现下载文件的防盗链 1.具体安装教程:http://www.cnblogs.com/tinywan/p/5983694. ...
- [转载] 深入理解Linux修改hostname
原文: http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问 ...
- Div里面载入另一个页面的实现(取代框架)(AJax)(转)
随着框架越来越不火了,HTML5就不对框架支持了,iframe也只有url了,Div就担当了此大任 DIV+CSS在页面部局确实也很让人满意,使用也更方便 今天突然遇到一个问题,那就是需要导入另一个页 ...