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 nthis 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
- 解题分析:根据题目描述需要在给定的区间里面找出循环长度最长的。
首先明确这个函数是什么?
函数f(n)为分段函数:当n为奇数时f(n)=3n+1;当n为偶数时f(n)=n/2;
然后需要明白什么是循环长度?
就是当f(n)进行多少次内部运算之后才能得到 1。
最后就是代码:
(1)写出函数计算循环长度;
(2)由于题目中没有说明i,j的大小关系,同时题目要求输出的i,j顺序和输入的i,j顺序要相同,所以可以有两种方法:
第一种:输入i,j之后直接输出,然后计算最长循环长度;
第二种:输入i,j之后,声明新的变量来使用,计算过程中完全不影响i,j的值。最后输出时i,j直接输出即可。
已过代码:
- #include <bits/stdc++.h>
- using namespace std;
- int length(int n) {
- int len=;
- while(n!=)
- {
- if(n%==)
- n=*n+;
- else
- n=n/;
- len=len+;
- }
- return len;
- }
- int main(void) {
- int start,over;
- int s,o;
- int ans;
- while(~scanf("%d%d",&start,&over))
- {
- ans=;
- s=start, o=over ;
- if(s>o) swap(s,o);
- for(int i=s;i<=o;i++)
- {
- int len=length(i);
- ans=max(ans,len);
- }
- printf("%d %d %d\n",start,over,ans);
- }
- }
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 (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 ...
- uva----(100)The 3n + 1 problem
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
随机推荐
- Google FlatBuffers——开源、跨平台的新一代序列化工具
前段时间刚试用了一个序列化工具cereal,请看cereal:C++实现的开源序列化库,打算再总结下我对google proto buf序列化库的使用呢, 结果还没动手,大Google又出了一个新的. ...
- IOS开发UI基础UILabel属性
UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; N ...
- 自定义JSTL标签和函数库
一.自定义JSTL标签 1.编写标签处理类: (1)实现 SimpleTag 接口,通过 setJspContext()方法可以获取到 jspContext 对象,实际上也是 pageContext ...
- js页面跳转参考代码大全
整理一下JS页面跳转参考代码 第一种: <script language=/"javascript/" type=/"text/javascript/&qu ...
- Winform开发框架之权限管理系统改进的经验总结(4)--用户分级管理
在实际的系统应用环境中,用户的分级管理一般也是比较常见的功能,小的业务系统可以不需要,但是一般涉及到集团.分子公司.或者是事业单位里面的各个处室或者某某局的人员管理,这些分级管理就显得比较必要,否则单 ...
- 客户关系管理系统(CRM)的开发过程中使用到的开发工具总结
开发<客户关系管理系统(CRM)>软件过程,也就是一个标准的Winform程序的开发过程,我们可以通过这个典型的软件开发过程来了解目前的开发思路.开发理念,以及一些必要的高效率手段.本篇随 ...
- 一个App的界面设计流程是怎么产生的
作者:候佩雯链接:http://www.zhihu.com/question/27088793 完整的流程,分层次设计,自下而上去完成: 策略层,定义产品使命.价值.目标人群 愿景/功能层:定义核心场 ...
- JavaScrip的DOM操作
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型,文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西 2.Windows对象操作 一.属性和方法 二.Window.open(& ...
- csharp: get Web.Services WebMethod
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- FreeBSD10上编译尝试DeepIn UI
经历了两百多次命令的输入尝试,终于搞定. 1 git clone https://github.com/linuxdeepin/deepin-ui.git 11 git clone https://g ...