DS1-13
Posted ozxics
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DS1-13相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #define MAXSIZE 10000 int Max3(int A, int B, int C); int DivideAndConquer(int List[], int left, int right); int MaxSubseqSum3(int List[], int N); int main() { int N; int List[MAXSIZE]; scanf("%d", &N); int i; for(i=0; i<N; ++i) scanf("%d", &List[i]); int ans=MaxSubseqSum3(List, N); printf("%d ", ans); return 0; } int Max3(int A, int B, int C) { return A>B?A>C?A:C:B>C?B:C; } int DivideAndConquer(int List[], int left, int right) { int MaxLeftSum, MaxRightSum; /*存放左右子问题的解*/ int MaxLeftBorderSum, MaxRightBorderSum; /*存放跨分界线的结果*/ int LeftBorderSum, RightBorderSum; int center, i; if(left==right) /*递归的终止条件,子列只有1个数字*/ if(List[left]>0) return List[left]; else return 0; /*下面是分的过程*/ center=(left+right)/2; MaxLeftSum=DivideAndConquer(List, left, center); MaxRightSum=DivideAndConquer(List, center+1, right); MaxLeftBorderSum=0, LeftBorderSum=0; for(i=center; i>=left; --i) { LeftBorderSum+=List[i]; if(LeftBorderSum>MaxLeftBorderSum) MaxLeftBorderSum=LeftBorderSum; } MaxRightBorderSum=0, RightBorderSum=0; for(i=center+1; i<=right; ++i) { RightBorderSum+=List[i]; if(RightBorderSum>MaxRightBorderSum) MaxRightBorderSum=RightBorderSum; } return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum+MaxRightBorderSum); } int MaxSubseqSum3(int List[], int N) { return DivideAndConquer(List, 0, N-1); }
以上是关于DS1-13的主要内容,如果未能解决你的问题,请参考以下文章