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.
方法一:
#include<stdio.h> #include<math.h> #include<string.h> #include<ctype.h> #include<stdlib.h> #include<stdbool.h> int powcount(long long n) //计算2的幂数 { int count=0; while(n>>=1) count++; return count; } bool ispower(long long v) //判断n是否为2的幂 { if(((v & (v - 1)) == 0)) return true; else return false; } int length(long long n) { int sum=1; while(1) { if(n==1) break; if((n & 1)==0) { if(ispower(n)) return sum+powcount(n); else n=n/2; } else n=3*n+1; sum++; } return sum; } int main() { int i,t,k,max=0; for(i=2; i<1000000; i++) { t=length(i); if(t>max) { max=t; k=i; } } printf("%lld\n",k); return 0; }
方法二:
#include<stdio.h> #include<math.h> #include<string.h> #include<ctype.h> #include<stdlib.h> #include<stdbool.h> int a[1000001]; void find() { long long i,j,k,f,sum,max=0; a[1]=1,a[2]=2; for(j=3; j<1000000; j++) { sum=1,k=i=j; while(1) { if((i & 1)==0) { i=i/2; if(i<k) { a[k]=sum+a[i]; break; } } else { i=3*i+1; } sum++; } if(a[k]>max) { max=a[k]; f=k; } } printf("%d\n",f); } int main() { find(); return 0; }
Answer:
|
837799 |
相关推荐
通过一系列测试用例,如:"fosh"和"fish"、"fish"和"hish"、"lucider"和"lucifer"、"hahaui"和"hfui"以及"sasa"和"fgdfrsa",检查`longestCommonSequence`函数的输出是否符合预期。 动态规划解决问题的核心在于将...
LMS Longest Monotonically Increasing Sequence Algorithm
python python_leetcode题解之128_Longest_Consecutive_Sequence
javascript js_leetcode题解之128-longest-consecutive-sequence.js
在这个压缩包文件"The-longest-public-son-sequence.rar_The Son"中,我们可以找到一个关于LCS的演示程序,可能包含了源代码、解释和实例,用于帮助理解这一算法。 LCS问题的基本概念是:给定两个字符串,找出它们...
最长公共子字符串共有两种解决方法,下面具体说说我的思路方法一:Longest Common Substring和Longest Common Subsequence是有区别的X = <a>Y = <a>X和Y的Longest Common Sequence为,长度为4X和Y的Longest Common ...
在提供的文件"longest_common_sub-sequence.c"中,我们可以看到C语言实现的LCS算法。这个程序首先定义了二维数组LCS来存储中间结果,然后通过两层循环遍历两个字符串的每个字符,根据上述状态转移方程计算LCS数组。...
js js_leetcode题解之14-longest-common-prefix.js
c语言入门 C语言_leetcode题解之14-longest-common-prefix.c
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
给定两个序列X={X1, X2,···,Xm}和Y={Y1, Y2,···,Yn},找出X和Y的最长公共子序列(Longest Common Sequence)。 比如字符串X:{BDCABA};字符串Y:{ABCBDAB},则这两个字符串的最长公共子序列长度为4,最长公共...
这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
最长连续序列 描述 给定一个未排序的整数数组,请找出最长的连续元素序列的长度。 好吧,假设该任务仅与步长等于1的序列有关。 例如: 给定exampleArray = [100, 4, 200, 1, 3, 2] 100,4,200,1,3,2 [100, 4, ...
This shortest common supersequence problem is closely related to the longest common subsequence problem. Given two sequences X = ,...,xm > and Y = ,...,yn >, a sequence U = ,...,uk > is a common ...
1022 Longest Common Sequence 也可用二叉搜索树(nlog时间)解决,见llj的书 1023 Happy Travel 转化为背包问题 1029 交点问题 据说有一个公式可以直接套 1031 分礼物 二分逼近,也可DP解决 1035 合法序列 1043 ...
Pku acm 第2533题 Longest Ordered Subsequence 代码,有详细的注释,动态规划
其核心算法基于高效的差分算法,如Longest Common Subsequence (LCS) 或者 O(ND) Diff算法,这些算法能够在较短的时间内找到两个序列的最小编辑距离,从而生成必要的变更指令。 Meteor Diff Sequence的主要功能...
最长公共子序列(Longest Common Subsequence, LCS)问题是计算机科学领域中一个重要的经典问题,它不仅被广泛应用于文本比较、生物信息学中的基因序列比对等领域,而且也是动态规划算法的一个典型应用场景。...