题目:输入一个正数n,输出所有和为n连续正数序列。
例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
分析:这是网易的一道面试题。
这道题和本面试题系列的第10题有些类似。我们用两个数small和big分别表示序列的最小值和最大值。首先把small初始化为1,big初始化为2。如果从small到big的序列的和大于n的话,我们向右移动small,相当于从序列中去掉较小的数字。如果从small到big的序列的和小于n的话,我们向右移动big,相当于向序列中添加big的下一个数字。一直到small等于(1+n)/2,因为序列至少要有两个数字。
基于这个思路,我们可以写出如下代码:
1 void PrintContinuousSequence(int small, int big); 2 3 / 4 // Find continuous sequence, whose sum is n 5 / 6 void FindContinuousSequence(int n) 7 { 8 if(n < 3) 9 return; 10 11 int small = 1; 12 int big = 2; 13 int middle = (1 + n) / 2; 14 int sum = small + big; 15 16 while(small < middle) 17 { 18 // we are lucky and find the sequence 19 if(sum == n) 20 PrintContinuousSequence(small, big); 21 22 // if the current sum is greater than n, 23 // move small forward 24 while(sum > n) 25 { 26 sum -= small; 27 small ++; 28 29 // we are lucky and find the sequence 30 if(sum == n) 31 PrintContinuousSequence(small, big); 32 } 33 34 // move big forward 35 big ++; 36 sum += big; 37 } 38 } 39 40 / 41 // Print continuous sequence between small and big 42 / 43 void PrintContinuousSequence(int small, int big) 44 { 45 for(int i = small; i <= big; ++ i) 46 printf("%d ", i); 47 48 printf("\n"); 49 }
以上转自何海涛博客
刚在《编程之美》2.21节看到这题,网上查了一下,发现了一个O(n^1/2)的算法,具体解析如下
我们知道:
1+2 = 3;
4+5 = 9;
2+3+4 = 9。
等式的左边都是两个以上连续的自然数相加,那么是不是所有的整数都可以写成这样的形式呢?稍微考虑一下,我们发现,4和8等数不能写成这样的形式。
问题1:写一个程序,对于一个64位的正整数,输出它所有可能的连续自然数(两个以上)之和的算式。
问题2:大家在测试上面的程序的过程中,肯定会注意到有一些数字不能表达为一系列连续的自然数之和,例如32好像就找不到。那么,这样的数字有什么规律呢?能否证明你的结论?
问题3:在64位正整数范围内,子序列数目最多的数是哪一个?这个问题要用程序蛮力搜索,恐怕要运行很长时间,能够用数学知识推导出来?
问题1解答:对于任意的正整数n >= 3(1和2均不能写成连续的自然数序列之和)。假设n能够写成自然数序列[seqStart, seqEnd]之和,则有(seqEnd + seqStart)*(seqEnd - seqStart + 1) = 2*n。考虑左式是两个整数之积,想到对右边的2*n进行因数分解,不妨假定2*n = minFactor * maxFactor,则有
seqEnd + seqStart = maxFactor (1)
seqEnd - seqStart = minFactor-1 (2)
解方程组(1)(2)得:
seqStart = (maxFactor - minFactor + 1) / 2
seqEnd = (maxFactor + minFactor - 1) / 2
因为maxFactor - minFactor与maxFactor + minFactor有相同的奇偶性,因此只需要判断maxFactor + minFactor的奇偶性即可,如果maxFactor + minFactor为奇数,那么seqStart和seqEnd不是分数,是整数,即这个序列存在。下面是代码:
1 int plusSequence(unsigned int n) 2 { 3 int count = 0; 4 int sqrtN = static_cast<int>(sqrt(static_cast<double>(2*n))); 5 for(int i = 2; i <= sqrtN; ++ i) 6 { 7 if(2*n % i == 0) // if the current i is the factor of 2*n 8 { 9 int minFactor = i; 10 int maxFactor = 2*n / i; 11 /* Judge if (minFactor + maxFactor) is odd, if it is, 12 * it means that the n is the sum of a natural number sequence 13 */ 14 if(((minFactor + maxFactor) & 0x00000001) == 1) 15 { 16 count ++; 17 int seqStart = (maxFactor - minFactor + 1) >> 1; 18 int seqEnd = (maxFactor + minFactor - 1) >> 1; 19 printf("count %d is closed interval [%d, %d]/n", count, seqStart, seqEnd); 20 } 21 } 22 } 23 return count; 24 }
问题二解答:
对于任意的奇数n = 2*k + 1(k >= 1),均可以写成[k, k + 1]之和,因此所有的奇数均满足条件。
对于每一个偶数,均可以分解为质因数之积,即n = pow(2, i)*pow(3, j)*pow(5,k)...,如果除了i之外,j,k...均为0,那么n = pow(2, k),对于这种数,其所有的因数均为偶数,观察上面的代码,14行要求两个因数之和为奇数,因此2的幂均不满足条件。对于非2的幂的偶数,均可以写成一个 奇数和偶数之积,因此满足14行的条件,说明可以写成一个连续的自然数之和。因此除了2的幂之外,所有的正整数n >=3均可以写成一个连续的自然数之和。
问题三解答:
前面就当是抛砖引玉,问题三还没有解答出来,如果大家有什么思路,可以联系我。
注:上面的程序并没有考虑整数很大,导致溢出的情况。
以上转自http://blog.youkuaiyun.com/wangpingfang/article/details/5893412