最大字段求和问题
[cpp]
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void maxSum(int *a,int n,int &max,int &mi,int &mj){
max=0;
mi=mj=0;
int b=0;
int p=0;
for(int i=0;i<n;i++){
if(b>0){
b+=a[i];
if(b>max){
max=b;
mi=p;
mj=i;
}
}else{
b=a[i];
p=i;
}
}
}
int main()
{
int a[]={2,-3,4,1,-2,5,-6};
int mi=0,mj=0,max=0;
maxSum(a,7,max,mi,mj);
cout<<"max value:"<<max<<endl;
cout<<"max sub sequence: ";
for(int i=mi;i<=mj;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
return 0;
}
补充:软件开发 , C++ ,