标签:
a1 |
a2 |
… |
ai |
… |
aj |
… |
an-1 |
an |
|
1 |
2 |
3 |
4 |
5 |
6 |
a[i] |
-2 |
11 |
-4 |
13 |
-5 |
-2 |
b(初值=0) |
-2 |
11 |
7 |
20 |
15 |
13 |
sum |
0 |
11 |
11 |
20 |
20 |
20 |
1 #include<iostream> 2 using namespace std; 3 4 int MaxSum(int n) 5 { 6 int sum = 0; 7 int b = 0; 8 for(int i=1; i<=n; i++) 9 { 10 if(b>0) b+=a[i]; else b=a[i]; 11 if(b>sum) sum = b; 12 } 13 return sum; 14 } 15 16 int main() 17 { 18 19 }
构造最优值:
1 #include<iostream> 2 using namespace std; 3 4 const int MAXN = 1001; 5 int a[MAXN]; 6 7 int MaxSum(int n, int &besti, int &bestj) 8 { 9 int sum = 0; 10 int b = 0; 11 int begin = 0; 12 for(int i=1; i<=n; i++) 13 { 14 if(b>0) b+=a[i]; 15 else 16 { 17 b=a[i]; begin = i; 18 } 19 if(b>sum) 20 { 21 sum = b; 22 besti = begin; 23 bestj = i; 24 } 25 } 26 return sum; 27 }
标签:
原文地址:http://www.cnblogs.com/acm1314/p/4578577.html