-
Notifications
You must be signed in to change notification settings - Fork 15
/
p18.c
51 lines (42 loc) · 1.2 KB
/
p18.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#define ElementType int
int maxSubsequenceSum(int A[], int N)
{
int thisSum, maxSum, i, j, k;
maxSum = 0;
for(i = 0; i < N; i++)
for(j = i; j < N; j++) { /* i划分出两个集合,j from i to N-1 */
thisSum = 0;
for(k = i; k <= j; k++) /* k from i to j , j在后半集合再划分集合 */
thisSum += A[k];
if(thisSum > maxSum)
maxSum = thisSum;
}
return maxSum;
}
/* 打印数组数据 */
void printArray(int* a, int N)
{
for(int i = 0; i < N; i++)
printf("%4d", a[i]);
printf("\n");
}
main()
{
int N = 8, maxSum = 0;
char p[256];
char *str = p;
ElementType A[] = {4, -3, 5, -2, -1, 2, 6, -2};
printf("\t\t\t ========== test for maximum sum of subsequence ==========\n\n");
str = "initial array A: ";
printf("%50s",str);
printArray(A, N);
str = "after computing maximum sum of subsequence: ";
printf("%50s", str);
maxSum = maxSubsequenceSum(A, N);
printArray(A, N);
str = "the maximum sum of array subsequence is: ";
printf("%50s", str);
printf("%4d\n\n", maxSum);
return 0;
}