The following iterative sequence is defined for the set of positive integers:

n n/2 (n is even) n 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 40 20 10 5 16 8 4 2 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

题目大意:

以下迭代序列定义在整数集合上:

n  n/2 (当n是偶数时) n  3n + 1 (当n是奇数时)

应用以上规则,并且以数字13开始,我们得到以下序列:

13  40  20  10  5  16  8  4  2  1

可以看出这个以13开始以1结束的序列包含10个项。虽然还没有被证明(Collatz问题),但是人们认为在这个规则下,以任何数字开始都会以1结束。

以哪个不超过100万的数字开始,能给得到最长的序列? 注意: 一旦序列开始之后,也就是从第二项开始,项是可以超过100万的。

方法1:

#include<stdio.h>
#include<math.h>
#include<stdbool.h> int powcount(long long n) //计算2的幂数
{
int count=;
while(n>>=) count++;
return count;
} bool ispower(long long v) //判断n是否为2的幂
{
if(((v & (v - )) == )) return true;
else return false;
} int length(long long n)
{
int sum=;
while()
{
if(n==) break;
if((n & )==)
{
if(ispower(n)) return sum+powcount(n);
else n=n/;
}
else n=*n+;
sum++;
}
return sum;
} int main()
{
int i,t,k,max=;
for(i=; i<; i++)
{
t=length(i);
if(t>max)
{
max=t;
k=i;
}
}
printf("%lld\n",k);
return ;
}

方法2:

#include<stdio.h>
#include<math.h>
#include<stdbool.h> int a[]; void find()
{
long long i,j,k,f,sum,max=;
a[]=,a[]=;
for(j=; j<; j++)
{
sum=,k=i=j;
while()
{
if((i & )==)
{
i=i/;
if(i<k)
{
a[k]=sum+a[i];
break;
}
}
else
{
i=*i+;
}
sum++;
}
if(a[k]>max)
{
max=a[k];
f=k;
}
}
printf("%d\n",f);
} int main()
{
find();
return ;
}
Answer:
837799

(Problem 14)Longest Collatz sequence的更多相关文章

  1. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  2. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  3. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  4. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  5. (Problem 53)Combinatoric selections

    There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...

  6. (Problem 49)Prime permutations

    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

  7. (Problem 47)Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2  7 15 = 3  5 The fi ...

  8. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

  9. (Problem 28)Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

随机推荐

  1. 利用python进行数据分析之数据规整化

    数据分析和建模大部分时间都用在数据准备上,数据的准备过程包括:加载,清理,转换与重塑. 合并数据集 pandas对象中的数据可以通过一些内置方法来进行合并: pandas.merge可根据一个或多个键 ...

  2. [LeetCode]题解(python):006-ZigZag Conversion

    题目来源: https://leetcode.com/problems/zigzag-conversion/ 题意分析: 这道题目是字符串处理的题目.输入一个字符串和一个数字,将字符串填入倒Z形输入字 ...

  3. Vistual Studio 2010 调试无法进断点

    系统是2003出现的问题 win8就没事 打sp1 补丁就行

  4. asp.net mvc重写RequestValidator

    /// <summary> /// <httpRuntime requestValidationType="xxx.CustomRequestValidator" ...

  5. Nginx 的 Echo 模块 —— echo-nginx-module(转)

    Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  6. centos 环境下monolog+php 方案

    1.在项目中,日志系统有多重要详细所有程序员都知道,monolog就是一个最好的解决方案,有各种级别,各种日志存储方式,具体可以上monolog官方了解http://monolog.ow2.org/ ...

  7. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  8. 关于QT中evaluateJavaScript()函数返回值的处理问题

    关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...

  9. Oracle EBS Web ADI 中的术语

    Oracle EBS Web ADI 中的术语 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 异步调用异步调用是这样子的,和引入接口表中的数 ...

  10. c 输出9x9乘法口诀表 这个学for循环绕不开的一题

    #include<stdio.h> int main(void) { int i,j; ;i<=;i++) { ;j<=i;j++) { printf("%d*%d= ...