pkusiro 12077 The 3n + 1 problem

ACM初学者 The 3n + 1 problemPOJ 1207翻译后大概是这个意思:考虑一个算法来生成以下序列的.从整数n开始.如果n为偶,除以2.如果是奇,乘3加1.对后面的n重复这个步骤,n = 1时终止.例如,n=22时,会产生下列顺序编号.22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 据猜想(但尚未证实),对任意整数n,该算法将终止在n = 1处.还有,此猜想对于1,000,000内的整数都成立.对于一个输入n,n的周期长度是指产生包括(1)在内的数字个数.在上面的例子中,22的周期长度为16.给定任何两个数字i和j,需要你确定出对于i和j之间数字的最大周期长度,包括i和j两端点.输入:输入将是一系列的整数对i和j,每行一个整数对.所有的整数将小于10000,且>0.输出:为每一对输入整数对i和j,输出的i和j应是同样的顺序,最后是的最大周期长度.这三个数字由一个空格隔开,每三个数字一行,对应一种输入和输出.Sample Input1 10100 200201 210900 1000Sample Output1 10 20100 200 125201 210 89900 我是这么写的:#includeint main(){int i,j,t,n,p,l;while(cin>>i>>j)l=1; //周期最小是1{if(i>j){t=i;i=j;j=t;}for(i>0;i
黎约傀儡组263
哈哈!其实有的时候很纠结.我用下面的代码在某校的OJ上过了(因为时间太长忘了),但在我们学校的OJ上去Wrong Answer.The 3n + 1 problem Consider the following algorithm to generate a sequence of numbers.Start with an integer n.If n is even,divide by 2.If n is odd,multiply by 3 and add 1.Repeat this process with the new value of n,terminating when n = 1.For example,the following sequence of numbers will be generated for n = 22:考虑一个算法来生成以下序列的.从整数n开始.如果n为偶,除以2.如果是奇,乘3加1.对后面的n重复这个步骤,n = 1时终止.例如,n=22时,会产生下列顺序编号.22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 It is conjectured (but not yet proven) that this algorithm will terminate at n = 1 for every integer n.Still,the conjecture holds for all integers up to at least.据猜想(但尚未证实),对任意整数n,该算法将终止在n = 1处.还有,此猜想对于1,000,000内的整数都成立.For an input n,the cycle-length of n is the number of numbers generated up to and including the 1.In the example above,the cycle length of 22 is 16.Given any two numbers i and j,you are to determine the maximum cycle length over all numbers between i and j,including both endpoints.对于一个输入n,n的周期长度是指产生包括(1)在内的数字个数.在上面的例子中,22的周期长度为16.给定任何两个数字i和j,需要你确定出对于i和j之间数字的最大周期长度,包括i和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 1,000,000 and greater than 0.输入,输入将是一系列的整数对i和j,每行一个整数对.所有的整数将少于100万,且>0.Output For each pair of input integers i and j,output i,j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j.These three numbers should be separated by one space,with all three numbers on one line and with one line of output for each line of input.输出为每一对输入整数对i和j,输出的i和j应是同样的顺序,中间是其间的最大周期长度.这三个数字由一个空格隔开,每三个数字一行,对应一种输入和输出.Sample Input 输入例:1 10100 200201 210900 1000Sample Output 1 10 20100 200 125201 210 89900 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1#includeint main(){int n1,n2,i=0,j,k=0,max,t=0,m1,m2;while(scanf("%d%d",&n1,&n2)==2){max=0;while(n){scanf("%d%d",&n1,&n2);}m1=n1;m2=n2;if(m>n){m1=n2;m2=n1;}j=m1;for(;j
为您推荐:
其他类似问题
扫描下载二维码3n+1 problem_百度知道算法(82)
The 3n + 1 problem
Time Limit:&1000MS
Memory Limit:&10000K
Total Submissions:&48403
Accepted:&15336
Description
Sample Input
Sample Output
100 200 125
201 210 89
#include &stdio.h&
//#include &stdlib.h&
int Cal(int n)
int count = 1;
while(n!=1){
if(n % 2 == 1){
n = 3*n+1;
count++;
int main()
int tmp,maximum,a, b,
while(scanf(&%d %d&,&a,&b)!=EOF){
if(a & b){
max =min =
max = min =
maximum = Cal(min);
for(i = min+1; i &= i++)
tmp = Cal(i);
if(maximum & tmp) maximum =
printf(&%d %d %d\n&,a, b ,maximum);
system(&pause&);
注意 输入的值是没有先后顺序的!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:202873次
积分:4172
积分:4172
排名:第5335名
原创:198篇
转载:15篇
评论:106条
文章:30篇
阅读:32906
(1)(3)(6)(15)(14)(23)(5)(32)(18)(1)(26)(28)(3)(1)(9)(6)(2)(2)(7)(7)(5)1624人阅读
C/C++(15)
/*题目三:The 3n + 1 problem(PKU 1207)*作者:国风崽 */#include&cstdio&int main(){&&& &int x,y;&&& &&&& while(scanf(&%d %d&,&x,&y)!=EOF){&&& &//while(cin&&x&&y){&&& &&& &int x1=x,y1=y;&&&&&& int maxLength=1;//存放最大 cycle length。 &&&&&& if(x1&y1){&&& //输入可能从大到小,但是输出时又不能改变顺序,所以多声明两个替代变量 &&& &&& &&&& & int t=x1;&&& &&& &&& &&& x1=y1;&&& &&& &&& &&& y1=t;&&& &&& &&&&&&& }&&&&&& for(int i=x1;i&=y1;++i){&&&& &&& &&& & int temp=i;&&&& &&& &&& & int length=1;//存放每个整数的 cycle length。&&&& &&& &&& & while(temp!=1){& &&& &&& &&& &&& &&& &if(temp%2==1){&&& &&& & &&& &&& &&&&&& temp=3*temp+1;&&& &&&& &&& &&& &&& }else{&&& &&&&&&&&&&&&&&&& temp=temp/2;&&& &&& &&& &&& &&& }&&& &&& &&& &&& &&& ++&&& &&& &&& & }&&& &&& &&& & maxLength=maxLength&length?maxLength://将大者存放到 maxLength 中 。 &&& &&& &}&&&&&& &&& &&& //cout&&x&&& &&&y&&& &&&maxLength&&&&& &&& printf(&%d %d %d/n&,x,y,maxLength);&&& &}&&& &return 0;}
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:91557次
积分:1482
积分:1482
排名:千里之外
原创:53篇
评论:45条
(1)(1)(3)(1)(3)(2)(3)(4)(1)(1)(11)(5)(3)(5)(1)(8)}

我要回帖

更多关于 siro 1207 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信