文章
3
粉丝
15
获赞
3
访问
2.7k
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100;
int a[N],s[N];
int main(){
int n;
while(cin>>n){//注意循环输入多组数据!!
int ans = -1e7;
int ans_i = 0;
for(int i = 1;i <= n;i++){
cin>>a[i];
if(ans < a[i]) {ans = a[i],ans_i = i;}
s[i] = s[i-1]+a[i];
}
int res = -1e7;
pair<int,int> res_ij;
for(int i = 1;i <= n-1;i++){
for(int j = 1;j + i<= n;j++){
int t = s[j+i] - s[j-1];// [j,j+i] [1,1+1]
if(res < t) {res = t;res_ij = {j,j+i};}
}
}
if(res >= ans)
for(int i = res_ij.first; i<= res_ij.second;i++)
cout<<a[i]<<" ";
else
{cout<<a[ans_i];res = ans;}
cout<<endl<<res<<endl;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发