I - The 3n + 1 problem(2.4.2)

Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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



题目大意:给出两个数求在两个数之间的有最大循环长度的数,最大循环长度利用题目所给的3n+1解决问题的方法求出。

#include<stdio.h>
#include <iostream>
int arr[10001];
using namespace std;
int main()
{
int a,b;
int i;
arr[1]=1;
arr[2]=2;
for(i=3;i<10001;i++)
{
long long temp=i;
int cnt=0;
while(1)
{
if(temp&(0x01))
{
temp*=3;
temp+=1;
cnt++;
}
else
{
cnt++;
temp>>=1;
}
if(temp<i)
{
arr[i]=cnt+arr[temp];
break;
}
}
}
int t;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a>b)
{
t=a;
a=b;
b=t;
}
int max=0;
for(i=a;i<=b;i++)
{
if(max<arr[i])
{
max=arr[i];
}
}
printf("%d %d %d\n",a,b,max);
}
return 0;
}

I - The 3n + 1 problem(2.4.2)的更多相关文章

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

  10. The 3n + 1 problem

    The 3n + 1 problem Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

随机推荐

  1. Swift udp实现根据端口号监听广播数据(利用GCDAsyncUdpSocket实现)

    有个小需求,app需要监听pc广播的数据: 代码实现思路: 使用三方库:CocoaAsyncSocket 1.开启udp监听: udpSocket.beginReceiving() 2.读取udp的数 ...

  2. Gitbook 命令行工具

    1.Gitbook 简介 1.1 Gitbook GitBook 是一个基于 Node.js 开发的命令行工具,使用它可以很方便的管理电子书,GitBook 是目前最流行的开源书籍写作方案. 使用 G ...

  3. IDEA使用笔记(六)——设置项目的JDK配置

    1:由于dev分支和master分支的代码差异比较多,所以,就从master上分出一个新的分支dev_,于是我就克隆新的代码,打开对应的项目文件,然后启动试试,发现报出如下的错误,很明显是因为没有制定 ...

  4. 12C新特性--Application Continuity

    Application Continuity特性可以在中断后恢复受影响的数据库会话的任务,从而让终端用户和应用程序感觉不到中断的发生.Application Continuity执行恢复的过程是在应用 ...

  5. R语言编程艺术#02#矩阵(matrix)和数组(array)

    矩阵(matrix)是一种特殊的向量,包含两个附加的属性:行数和列数.所以矩阵也是和向量一样,有模式(数据类型)的概念.(但反过来,向量却不能看作是只有一列或一行的矩阵. 数组(array)是R里更一 ...

  6. Socket网络编程--网络爬虫(1)

    我们这个系列准备讲一下--网络爬虫.网络爬虫是搜索引擎系统中十分重要的组成部分,它负责从互联网中搜集网页,采集信息,这些网页信息用于建立索引从而为搜索引擎提供支持,它决定着整个引擎系统的内容是否丰富, ...

  7. C++11 右值引用和转移语义

    新特性的目的 右值引用 (Rvalue Referene) 是 C++ 新标准 (C++11, 11 代表 2011 年 ) 中引入的新特性 , 它实现了转移语义 (Move Sementics) 和 ...

  8. SpringMVC(Springboot)返回文件方法

    https://blog.csdn.net/Lynn_coder/article/details/79953977 ****************************************** ...

  9. idea Connection to SQL Server - 公网8 failed java

    Connection to SQL Server - 公网8 failed java.sql.SQLException: I/O Error: SSO Failed: Native SSPI libr ...

  10. asp.net Request、Request.Form、Request.QueryString的区别(转)

    Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...