链接:点击打开链接
题意:求循环数组的最大子序列和
代码:
#include <iostream> using namespace std; long long num[100005]; int main() { long long n,i,sum1,sum2,summ,maxx,maxx1,maxx2; while(cin>>n) { sum1=sum2=summ=maxx1=maxx2=0; for(i=1;i<=n;i++) { cin>>num[i]; sum1+=num[i]; if(sum1<0) sum1=0; else if(sum1>maxx1) maxx1=sum1; summ+=num[i]; sum2-=num[i]; if(sum2<0) sum2=0; else if(sum2>maxx2) maxx2=sum2; //同时求出最大子序列和和最小子序列和 } //cout<<maxx1<<" "<<summ<<" "<<maxx2<<endl; maxx=max(maxx1,summ+maxx2); cout<<maxx<<endl; } return 0; }