专栏首页专注研发poj-1207 THE 3n+1 problem

poj-1207 THE 3n+1 problem

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. (是i和j之间数的循环次数最大的)

#include<iostream>
using namespace std;
int  bb(int i)
{     int count=1;
while(i!=1)
{
        if(i%2==0)i/=2;
        else i=i*3+1;
        count ++;


}

return count;

}
int main()
{

int a,b;
int q=0;
while(cin>>a>>b)
{
q++;
if(q>=10000)break;
int d=a>b?a:b;
int c=a<b?a:b;
int maxa=0;
for(int j=c;j<=d;j++)
{
int  a1=bb(j);
if(maxa<a1)maxa=a1;
}
cout<<a<<" "<<b<<" "<<maxa<<endl;


}

return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • poj-1028 -网页导航

    Standard web browsers contain features to move backward and forward among the pa...

    瑾诺学长
  • poj-3660-cows contest(不懂待定)

    N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a program...

    瑾诺学长
  • poj-1017-packets

    A factory produces products packed in square packets of the same height h and of...

    瑾诺学长
  • 浙江大学计算机与软件学院2019年保研上机模拟练习

    A happy number is defined by the following process: Starting with any positive i...

    glm233
  • HDUOJ---2642Stars(二维树状数组)

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Jav...

    Gxjun
  • Android LayoutInflater的用法详解

    相信我们在开发过程中肯定接触过LayoutInflater,比如ListView的适配器里的getView方法里通过LayoutInflater.from(Co...

    萬物並作吾以觀復
  • 机器人控制器编程课程-教案02-基础

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    zhangrelay
  • Codeforces 833E Caramel Clouds

    E. Caramel Clouds time limit per test:3 seconds memory limit per test:256 megaby...

    Angel_Kitty
  • hackerrank challenges median

    The median of M numbers is defined as the middle number after sorting them in or...

    xindoo
  • 为什么SpringBoot2.x官方首推Caffeine作为缓存框架?

    Caffeine是一个高性能,高命中率,低内存占用,near optimal 的本地缓存,简单来说它是Guava Cache的优化加强版,有些文章把Caffei...

    业余草

扫码关注云+社区

领取腾讯云代金券