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