题目1011:最大连续子序列(需要标记边界位置)
题目链接:http://ac.jobdu.com/problem.php?pid=1011
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
/ // 1011 最大连续子序列.cpp // Jobdu // // Created by PengFei_Zheng on 28/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cstring> #include <cmath> #define MAX_SIZE 10010 using namespace std; int k; int a[MAX_SIZE]; int main(){ while(scanf("%d",&k)!=EOF && k!=0){ for(int i = 0 ; i < k ; i++){ scanf("%d",&a[i]); } int sum = a[0], max = a[0]; int left =0 , right = 0; int ansLeft = 0, ansRight = 0; int ansMax = max; for(int i = 1 ; i < k ; i++){ if(sum < 0){ sum = 0; left = right = i; } sum += a[i]; right = i; if(sum > max){ ansLeft = left; ansRight = right; max = sum; ansMax = max; } } if(ansMax < 0){ printf("0 %d %d\n",a[0],a[k-1]); } else{ printf("%d %d %d\n",ansMax,a[ansLeft],a[ansRight]); } } } /************************************************************** Problem: 1011 User: zpfbuaa Language: C++ Result: Accepted Time:20 ms Memory:1560 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.