classnull100 - The 3n + 1 problem
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正
The 3n + 1 problem |
Background
Problems in Computer Science are often classified as belonging to acertain class of problems (e.g., NP, Unsolvable, Recursive). In thisproblem you will be analyzing a property of an algorithm whoseclassification 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 printed22 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 isprinted) for any integralinput value. Despite the simplicity of the algorithm,it is unknown whether this conjecture is true. It has been verified,however, for all integersn such that 0 < n < 1,000,000 (and, in fact,for many more numbers than this.)
Given an input n, it is possible to determinethe number of numbers printed (including the 1). For a givenn this iscalled thecycle-length of n. In the example above, the cyclelength of 22 is 16.
For any two numbers i and j you are to determine the maximum cyclelength over all numbers betweeni andj.
记不清有多少个夜晚,在我翻阅纸张的指间滑落;记不清有多少支蜡烛,在我的凝视中化为灰烬。逝者如斯,我时时刻刻会听见自己对生命承诺的余音,感到岁月的流转在渐渐稀释我的年少无知,我愿自己是一只上足了发条的时钟,在昼夜不停的流转中留下自己充实的每一刻。
The Input
The input will consist of a series of pairs of integers i and j, one pair ofintegers per line. All integers will be less than 1,000,000 and greaterthan 0.
You should process all pairs of integers and for eachpair determine the maximum cycle length over all integers between andincludingi andj.
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 includingi andj. These three numbersshould be separated by at least one space with all three numbers on oneline and with one line of output for each line of input. The integersi andj must appear in the output in the same order in which theyappeared in the input and should befollowed 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
- #include<stdio.h>
- int test(int n)
- {
- int t=1;
- while(n!=1){
- if(n%2)
- {n=3*n+1;t++;}
- else
- {n/=2;t++;}
- }
- return t;
- }
- int main(void)
- {
- int i,j,n;
- while(scanf("%d%d",&i,&j)==2)
- {
- int max=0;
- if(i<j){
- for(n=i;n<=j;n++)
- {
- if(test(n)>max)
- max=test(n);
- }}
- if(j<i){
- for(n=j;n<=i;n++)
- {if(test(n)>max)
- max=test(n);}
- }
- if(j==i){
- max=test(i);
- }
- printf("%d %d %d\n",i,j,max);
- }
- return 0;}
文章结束给大家分享下程序员的一些笑话语录: 话剧:程序员过沟
本剧内容纯属虚构,如有雷同……HEHE……俺也没办法了。
话说某市街道改建,某某软件公司门口横七竖八挖了几条大沟。一群程序员(SDK程序员赵某,VB程序员钱某,VC程序员孙某,DELPHI程序员李某)下班从公司里出来,看到门前的几条沟,于是各显神通……门前第一条沟也就半米来宽,SDK程序员赵某二话没说,轻轻一跃跳了过去,看到其它人纷纷把随身携带的公文包(类库)横在沟上踩着过沟,不屑地说,这么小一条沟,犯得着小题大做用那个吗?看我多么轻松多么洒脱多么……多么……(众人皆怒目横视之……)
接着第二条沟有点宽度。SDK程序员赵某还是还是一马当先,飞跃而起……不好,还差一点才到……幸好凭着多年的(跳远?编程?)经验,单手抓住沟沿,颤巍巍地爬了上来,嘴里还念念有词“高手就是高手啊,虽然差一点就……不过毕竟……HEHE……跳远是过沟的基础嘛,有基础(SDK)就有一切的说……”(众人作瞠目结舌状……)看到别人跳过去了,可自己又跳不了那么远,只好再想办法了……VB程序员钱某,DELPHI程序员李某打开手提,连上手机,开始上网找可供过沟的控件……VC程序员孙某却不慌不忙,打开公文包,把几块衬板拆了下来,然后三下五除二拼成一个简易木桥……“虽然这几个板子(类)做得不怎么样,不过先把这个项目应付过去,有时间我自己做一个好了……”于是踩着板子过了沟。
这时钱某和李某也分别找到了合适的东东。钱某找到的是“钢丝绳.ocx”,安装简单,使用方便,拉出一头,对孙某说“大虾,顺手拉兄弟一把……”,于是把绳子系在沟两边的绿化树木上,踩着钢丝就过了沟。刚刚站稳就四方作揖,“小生这里有礼了”。这时一戴着黄袖圈的老太太跳了出来,抓住钱某,“破坏绿化树木,罚款XXXX元,交钱,交钱,交钱!”(老人家作双枪老太婆怒视伪军状
……钱某被逼无奈,只好边掏钱,边对着后台叫道“导演,我这可是因公牺牲,不给个烈士称号也得报销”,后台一个臭鸡蛋飞出,“叫什么叫,我这个月的粮饷还不知哪里去领呢,都什么时代了,你不下岗都不错了……”)
李某看着刚刚好不容易从台湾拖回来的“铝条.ZIP”
---------------------------------
原创文章 By
class和null
---------------------------------
classnull100 - The 3n + 1 problem的更多相关文章
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 烟大 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 ...
- 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 ...
- uva----(100)The 3n + 1 problem
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 问题)——(离线计算)
// The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...
- 100-The 3n + 1 problem
本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...
- 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 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
- The 3n + 1 problem
The 3n + 1 problem Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
- Java-DES算法加密解密工具类
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import ...
- PHP 14:类的实例
原文:PHP 14:类的实例 在本章中我们将介绍一个实例,来进一步对类加深了解.本章构建一个Page类,它代表了一个页面.其中有显示页面的title,样式,内容等函数.此页面的效果如下:我们将把这个页 ...
- 慧都十年大促起幕,Dev、BCG等明星控件6.8折起!
2013慧都十周年大促正式起幕,DevExpress.BCGControlBar.FastReport.TeeChart等精选明星控件Top 10悉数"价"到,还有更多产品惊喜&q ...
- 【蜗牛—漫漫IT路之大学篇(九)
】
再来一篇叨叨的博客 近期,状态还是那个状态,人还是那个人. 前两天,感冒了,可能是宿舍阴面的事吧.然后,中午睡觉的时候穿着短袖披了一件外套,然后鼻子就不通气了.只是,前天晚上,我骑着崔国强的车子跑了不 ...
- 基于EF+MVC+Bootstrap的通用后台管理系统及架构
分享基于EF+MVC+Bootstrap的通用后台管理系统及架构 基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通用配置及服务调用, 提供了 ...
- jboss7.1.0配置数据库(mysql)
以jboss7.1.0和mysql为例 1.找到安装目录,我的目录是:jboss-as-7.1.0.CR1b 2.找到standalone.xml,这个文件在我的电脑上是C:\Users\dan\De ...
- ASP.NET MVC 5项目
图文详解远程部署ASP.NET MVC 5项目 话外篇: 由于感觉自己的机器比较慢,配置不好,所以最近想把之前的项目部署到实验室的服务器上,但是由于常不在实验室,所以在想能不能远程部署.因此今天专 ...
- 动手Jquery插件
自己动手Jquery插件 最近Web应用程序中越来越多地用到了JQuery等Web前端技术.这些技术框架有效地改善了用户的操作体验,同时也提高了开发人员构造丰富客户 端UI的效率.JQuery本身提供 ...
- 如何解决 Django中出现的 [Errno 13] Permission denied问题
环境:linux 如果你使用了Apache部署了Django项目,在上传文件时可能会出现 “[Errno 13] Permission denied:某目录”的错误. 这是因为apache没有权限在该 ...
- 使用Vim进行开发
从士兵到程序员再到SOHO程序员 (二) 原文地址: http://blog.huhao.name/blog/2013/12/13/become-a-freelancer-2/ 作者:胡皓 Blo ...