The 3n + 1 problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 60496   Accepted: 19218

Description

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

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 10,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.

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:题不难,但是改了好多次才ac的。

1.要注意输入的两个数不一定是i<j的,需要排序。

2.最后输出的i和j要跟刚开始输入的顺序一致,不能输出排序之后的。

#include <iostream>
#include <cstdio> using namespace std; int cal(int t){
int countt=;
while(t!=){
countt++;
if(t&==){
t=*t+;
}else{
t/=;
}
}
return countt;
} int main()
{
int a,b;
int maxx=;
int noww=;
while(~scanf("%d %d",&a,&b)){
cout<<a<<' '<<b<<' ';
if(a>b){
a^=b;
b^=a;
a^=b;
}
maxx=;
for(int i=a;i<=b;i++){
noww=cal(i);
maxx=noww>maxx? noww:maxx;
}
cout<<maxx<<endl;
}
return ;
}

poj1207 3n+1 problem的更多相关文章

  1. UVa 100 - The 3n + 1 problem(函数循环长度)

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

  2. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  3. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

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

     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 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  6. 100-The 3n + 1 problem

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

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

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

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

  9. classnull100 - The 3n + 1 problem

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正  The 3n + 1 problem  Background Problems in Computer Science are o ...

随机推荐

  1. HBase读写的几种方式(一)java篇

    1.HBase读写的方式概况 主要分为: 纯Java API读写HBase的方式: Spark读写HBase的方式: Flink读写HBase的方式: HBase通过Phoenix读写的方式: 第一种 ...

  2. MyBatis集成到Spring时配置MapperScannerConfigurer出错

    问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...

  3. mpvue体验微信小程序开发

    微信小程序 https://developers.weixin.qq.com/miniprogram/introduction/index.html?t=18082114 微信小程序是一种全新的连接用 ...

  4. Codeforces 1101G(线性基)

    题目链接 题意 将序列尽可能分成多段使得任意$x \geq 1$段内的所有元素的异或和大于$0$问最多多少段 思路 首先,如果所有元素异或和等于$0$答案显然为$-1$,否则构造整个序列的线性基,这个 ...

  5. 大家都知道fastclick能解决300ms延迟,现在我们来看一下,使用方法

    1.在终端输入以下命令进行安装 npm install fastclick -S 2.在你用脚手架搭建好的项目中,找到mian.js这个入口文件,打开 3.在其中加入: import FastClic ...

  6. 版本控制工具 - Git

    版本控制工具 - Git 安装完成后,打开Git Bash,这是一个命令行工具,用于操作仓库和仓库的文件.你可以通过命令将已经存在的项目变成仓库,也可以重新创建一个新项目再通过命令将其变成仓库,还可以 ...

  7. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

  8. Windows 下安装Git工具及基础使用

    Git简介 git是很好一个工具使用,可以执行liunx命令,有git环境后windows系统就可以进行shell命令操作,就可以添加其他liunx辅助软件进行执行,git也代码库管理工具,无论是上传 ...

  9. 使用Python正则表达式自己实现解析URL各参数

    不使用库,自己可以利用正则表达式实现解析域名各部分参数:协议.域名.端口.路径.载荷等. 使用的正则表达式如下: r'''(?x)\A ([a-z][a-z0-9+\-.]*):// # Scheme ...

  10. IIS无法删除应该程序池 因为它包含X个应用程序

    今天代码主分支在vs2015创建了虚拟目录http://localhost/webapp指向的物理路径是E:\webapp 之后新开了一个分支把代码放在了D:\webapp之后又在vs2015中创建了 ...