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

 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 ij, 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(函数循环长度)的更多相关文章

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

    100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...

  2. uva 100 The 3n + 1 problem (RMQ)

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

  3. 【转】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 / ...

  4. 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 ...

  5. UVa Problem 100 The 3n+1 problem (3n+1 问题)

    参考:https://blog.csdn.net/metaphysis/article/details/6431937 #include <iostream> #include <c ...

  6. 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+ ...

  7. 100-The 3n + 1 problem

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

  8. uva----(100)The 3n + 1 problem

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

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

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

随机推荐

  1. Shell基础整理

     Shell的作用是将用户输入的文本命令转换成内核能识别的数据指令交给内核进行执行,内核需要翻译成二进制交由CPU底层来执行     用户层->Shell->调用对应应用程序->ke ...

  2. Rest(Restful)风格的Web API跟RPC风格的SOAP WebService--这些名词都啥意思?

    经常看到这些词汇,也有baidu或google过,但记忆里总是模糊,不确定,以至于别人问及的时候,总说不清楚.开篇随笔记录下.大家有补充或者意见的尽请留文. 本文顺序: 一.Rest(Restful) ...

  3. mysql 行锁一则

       CREATE TABLE `t1` (  `id` int(11) NOT NULL DEFAULT '0',  `name` varchar(20) DEFAULT NULL,  PRIMAR ...

  4. Json的序列化和反序列化

    1.利用js进行序列化成字符串和反序列化 var personObj = {name:"Tom",age:16}; // 利用JS序列化成字符串 var personStr = J ...

  5. Android读写SD卡

    SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...

  6. SQL Server 2008 评估期已过解决方法

    SQL Server 2008有180天的试用期,过期后会提示“评估期已过”的提示. 1.进入SQL Server安装中心: 2.选择“维护”-“版本升级” 3.输入密钥: 其他的根据提示操作. 附S ...

  7. MVC 5 + EF6 入门完整教程14 -- 动态生成面包屑导航

    上篇文章我们完成了 动态生成多级菜单 这个实用组件. 本篇文章我们要开发另一个实用组件:面包屑导航. 面包屑导航(BreadcrumbNavigation)这个概念来自童话故事"汉赛尔和格莱 ...

  8. [转载]Ubuntu14.04 LTS更新源

    不同的网络状况连接以下源的速度不同, 建议在添加前手动验证以下源的连接速度(ping下就行),选择最快的源可以节省大批下载时间. 首先备份源列表: sudo cp /etc/apt/sources.l ...

  9. 重新想象 Windows 8 Store Apps (57) - 本地化和全球化

    [源码下载] 重新想象 Windows 8 Store Apps (57) - 本地化和全球化 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 本地化和全球化 本地化 ...

  10. 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInformation

    [源码下载] 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInfor ...