[Topic] enter an integer to output all continuous positive number sequences with the sum of n. For example, input 15. Because 15 = 7 + 8 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5, the output sequence is "1, 2, 3, 4, 5 ";" 4, 5, 6 "," 7, 8.
[Idea 1] from the viewpoint of the arithmetic difference series: if there is a number of columns that meet the requirements of I + (I + 1) +... + j = n. According to the summation formula of the arithmetic difference series, we can easily obtain: (I + j) (j-I + 1)/2 = n, that is, (I + j) (j-I + 1) = 2n. Since I and j are all positive integers, we can easily obtain (I + j) and (j-I + 1) All are positive integers, therefore, we can traverse all the 2n factors from 2 to sqrt (2n). For the factor k, we have:
We only need to ensure that the j and I values are all integers and I <j. Based on this idea, we have the following code:
1 # include <iostream>
2 # include <string>
3 # include <cmath>
4 using namespace std;
5
6 void FindContinuousSequence (int n)
7 {
8 int half = (int) (sqrt (2 * n ));
9 for (int k = 2; k <= half; ++ k)
10 {
11 // find the factor k
12 if (2 * n) % k = 0)
13 {
14 int temp1 = 2 * n-k * k + k;
15 int temp2 = 2 * n + k * k-k;
16
17 // start, and end are all Integers
18 if (temp1% (2 * k) = 0 & temp2 % (2 * k) = 0)
19 {
20 int start = temp1/(2 * k );
21 int end = temp2/(2 * k );
22
23 // print all numbers from start to end
24 for (int I = start; I <= end; ++ I)
25 cout <I <"\ t ";
26
27 cout <endl;
28}
29}
30}
31}
32
33 int main ()
34 {
35 cout <"Enter your Number:" <endl;
36 int number = 0;
37 cin> number;
38
39 cout <"the sum equals your number is:" <endl;
40 FindContinuousSequence (number );
41 return 0;
42}
The running result is as follows:
It is easy to see that this algorithm needs to traverse the range from 2-sqrt (2n), so the time complexity is O (sqrt (n), and the efficiency is relatively high, however, the disadvantage is that a lot of multiplication, division, and multiplication operations are required. For a large input with n values, the calculation process is relatively slow.
[Idea 2] We will consider this problem from another perspective. According to the basic idea of [algorithm 10] that tries to force the solution between the two ends, we can consider this as follows: we use small to indicate the minimum value in the sequence, and big to indicate the maximum value in the sequence. Because the sequence for n requires at least two numbers, the small value ranges from 1 to the midpoint; if small + big <n, let big move backward to increase sum; if small + big> n, let small move forward to reduce sum; if small + big = n, print all values from small to big. The code based on this idea is:
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 void FindContinuousNumbers(int n)
6 {
7 int small = 1;
8 int big = 2;
9 int middle = (n + 1)/2;
10 int sum = small + big;
11 while(small < middle)
12 {
13 if(sum == n)
14 {
15 for(int i = small;i <= big;++i)
16 cout<<i<<"\t";
17 cout<<endl;
18 }
19
20 while(sum > n)
21 {
22 sum -=small;
23 small++;
24
25 if(sum == n)
26 {
27 for(int i = small;i <= big;++i)
28 cout<<i<<"\t";
29
30 cout<<endl;
31 }
32
33 }
34
35 big++;
36 sum += big;
37 }
38 }
39
40 int main()
41 {
42 cout<<"Enter your Number:"<<endl;
43 int number = 0;
44 cin>>number;
45
46 cout<<"The sum equals your number is as following:"<<endl;
47 FindContinuousNumbers(number);
48
49 return 0;
50 }
The running result is as follows:
Efficiency Analysis: the small pointer should be traversed from 1 to middle, while the big pointer will always traverse forward for each small pointer, without pointing back to the pointer, therefore, the time complexity of the entire algorithm is O (N), which is less efficient than the previous algorithm. However, all operations are simple addition and subtraction, which is an advantage compared to the previous multiplication operations.
References:
He Haitao blog: http://zhedahht.blog.163.com/blog/static/25411174200732711051101/
Note:
1) All the code environments of this blog are compiled in win7 + VC6. All code has been debugged by the blogger.
2) The blogger python27 has the copyright to this blog post. For the reposted website, please indicate the source at http://www.cnblogs.com/python27 /. You have any suggestions for solving the problem. Please feel free to comment on them.