0
点赞
收藏
分享

微信扫一扫

连续子数组的最大和+如何处理以字符为分隔符的字符串


题目描述

一个非空整数数组,选择其中的两个位置,使得两个位置之间的数和最大。

如果最大的和为正数,则输出这个数;如果最大的和为负数或0,则输出0

输入描述:


3,-5,7,-2,8


输出描述:


13


示例1

输入

复制


-6,-9,-10


输出

复制


0


#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
int main(){
vector<int> Input;
while(1){
int temp;
cin >> temp;
Input.push_back(temp);
char c;
cin.get(c);
if(c == '\n'){break;}
}
int MAX = Input[0];
int temp = Input[0];
for(int i = 1;i < Input.size();i++){
temp = max(temp + Input[i],Input[i]);
if(temp > MAX){
MAX = temp;
}
}
if(MAX >= 0){
printf("%d\n",MAX);
}
else{
printf("0\n");
}
return 0;
}

 

举报

相关推荐

0 条评论