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:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdbool.h>
  4.  
  5. int powcount(long long n) //计算2的幂数
  6. {
  7. int count=;
  8. while(n>>=) count++;
  9. return count;
  10. }
  11.  
  12. bool ispower(long long v) //判断n是否为2的幂
  13. {
  14. if(((v & (v - )) == )) return true;
  15. else return false;
  16. }
  17.  
  18. int length(long long n)
  19. {
  20. int sum=;
  21. while()
  22. {
  23. if(n==) break;
  24. if((n & )==)
  25. {
  26. if(ispower(n)) return sum+powcount(n);
  27. else n=n/;
  28. }
  29. else n=*n+;
  30. sum++;
  31. }
  32. return sum;
  33. }
  34.  
  35. int main()
  36. {
  37. int i,t,k,max=;
  38. for(i=; i<; i++)
  39. {
  40. t=length(i);
  41. if(t>max)
  42. {
  43. max=t;
  44. k=i;
  45. }
  46. }
  47. printf("%lld\n",k);
  48. return ;
  49. }

方法2:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdbool.h>
  4.  
  5. int a[];
  6.  
  7. void find()
  8. {
  9. long long i,j,k,f,sum,max=;
  10. a[]=,a[]=;
  11. for(j=; j<; j++)
  12. {
  13. sum=,k=i=j;
  14. while()
  15. {
  16. if((i & )==)
  17. {
  18. i=i/;
  19. if(i<k)
  20. {
  21. a[k]=sum+a[i];
  22. break;
  23. }
  24. }
  25. else
  26. {
  27. i=*i+;
  28. }
  29. sum++;
  30. }
  31. if(a[k]>max)
  32. {
  33. max=a[k];
  34. f=k;
  35. }
  36. }
  37. printf("%d\n",f);
  38. }
  39.  
  40. int main()
  41. {
  42. find();
  43. return ;
  44. }
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. 高质量程序设计指南C/C++语言——C++/C程序设计入门(2)

    *标准C规定,编译器只取前31个字符作为有效的标识符,而标准C++则取前255个字符作为有效的标识符. *把具有特殊含义的字符输出到终端上,尤其是当它们出现在普通字符串或格式控制字符串中时,一般来说有 ...

  2. 我被SQL注入撞了一下腰

    网站的注入漏洞,应该说绝大多数做web开发的人都知道的事情.可是没想到从事6,7年开发工作的我,却会在这上栽了跟头,真是郁闷啊.心情很纠结,按照老婆的话,怎么感觉我像失恋了一样. 事情的起因还是在几个 ...

  3. UUID Gen

    https://github.com/twitter/snowflake/releases/tag/snowflake-2010 http://boundary.com/blog/2012/01/12 ...

  4. php浮点数计算比较及取整不准确解决方法

    原文:php浮点数计算比较及取整不准确解决方法 php有意思的现象,应该是很多编程语言都会有这样的现象.这个是因为计算机的本身对浮点数识别的问题..... $f = 0.58; var_dump(in ...

  5. JAVA GUI学习 - JTable表格组件学习_A ***

    public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...

  6. Java学习之DBUtils工具的学习

    简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...

  7. GTW likes math(简单数学)

    GTW likes math  Accepts: 472  Submissions: 2140  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  8. Angular JS 学习笔记(一)

    1. 菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 2. Angular JS中文网:http://www.apjs.net ...

  9. 查看Oracle数据库某用户的连接信息

    执行以下语句可查出用户TJAMIS_LXF连接信息: select schemaname, osuser, process, machine, port, terminal, program from ...

  10. shell 脚本中$$,$#,$?分别代表什么意思?

    $0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...