PAT甲级1007
来源:互联网 发布:网络用语略略什么意思 编辑:程序博客网 时间:2024/06/05 07:28
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int k;
scanf("%d",&k);
int i,j,temp=0,left=0,right=0,sum=-1,flag=1,index=0;
vector<int > num;
for(i=0;i<k;i++)
{ scanf("%d",&j);
num.push_back(j);
temp+=j;
if(j>=0) flag=0;//有大于零的数就标记
if(temp>sum) { //更新最大子列和
sum=temp;
left=index;
right=i;
}
else if(temp<0){ //小于零就不用再计算这一串子列和了,计算下一个(从正数)开始的子列
index=i+1; //这一时刻temp《0,始终将下一个数当作下一个子列的开始,index为下一个数的下标
temp=0; //置零计算下一个子列和
}
}
if(flag) printf("0 %d %d",num[0],num[k-1]); //所有数都为负数
else {
printf("%d %d %d",sum,num[left],num[right]);}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int k;
scanf("%d",&k);
int i,j,temp=0,left=0,right=0,sum=-1,flag=1,index=0;
vector<int > num;
for(i=0;i<k;i++)
{ scanf("%d",&j);
num.push_back(j);
temp+=j;
if(j>=0) flag=0;//有大于零的数就标记
if(temp>sum) { //更新最大子列和
sum=temp;
left=index;
right=i;
}
else if(temp<0){ //小于零就不用再计算这一串子列和了,计算下一个(从正数)开始的子列
index=i+1; //这一时刻temp《0,始终将下一个数当作下一个子列的开始,index为下一个数的下标
temp=0; //置零计算下一个子列和
}
}
if(flag) printf("0 %d %d",num[0],num[k-1]); //所有数都为负数
else {
printf("%d %d %d",sum,num[left],num[right]);}
return 0;
}
阅读全文
0 0
- PAT(甲级)1007
- PAT甲级1007
- PAT 甲级1007
- PAT-甲级-1007
- PAT甲级1007
- PAT甲级1007
- PAT甲级 1007
- 浙大PAT甲级-1007
- 【PAT】甲级1007
- PAT甲级 1007
- PAT 甲级
- PAT 1007(甲级)Maximum Subsequence Sum
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- [笔记]: 二叉树 遍历转换
- 计算几何题单整理
- 数据结构
- 超详细Redis数据库入门教程
- 使用ssm框架+pgsql
- PAT甲级1007
- 解决eclipse marketplace下载速度慢,proxy代理设置搞掂
- Neural Architectures for Named Entity Recognition
- Linux 下tomcat设置JVM参数
- P1149 火柴棒等式
- HDU2112(map+Dijstra最短路)
- STM32-步进电机S型加减速控制
- OpenCV的k
- 欢迎使用CSDN-markdown编辑器