PTA 1007 Maximum Subsequence Sum
/*
注意点:
1.输入数据若全是负数,输出0,以及整个序列的首尾的数字。
2.要注意最大子序列和!=最大连续递增子序列。
最大连续递增子序列:从i~n,一旦综合开始变小就停止循环,用数组记录i当前到后可能的最大子序列即可。
最大子序列和:特点:可能会先减小再增大值。
从头走到最后,不要停下,只记录最大值,并且同时改变start,end即可。(针对这道题而言。)
数据:
这个题的输出数据顺序,含义依次是:
最大子列和 最大子列和起始位置的那个数字 最大子列和终结位置的那个数字
10
-10 0 7 -2 -1 8 4 -100 30 2
32 30 210
-10 1 2 3 4 -5 -23 3 7 -21
10 1 410
0 0 0 0 0 0 0 0 0 0
0 0 010
1 2 3 4 5 -1 -2 -3 -4 -5
15 1 510
1 -2 3 4 -8 6 0 7 -9 10
14 6 10//重点标记这组数据。
5
-1 -2 -3 -4 -5
0 -1 -55
-100 99 -300 200 -500
200 200 2005
-100 -200 0 -300 -500
0 0 0
*/
#include"stdafx.h"
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int a[10005], b[10005];
int main()
{int n, i, j, maxnow, maxyet, maxend = -1e9, start, end;cin >> n;int flag = 0;//为了判定是否全为负数。for (i = 0; i < n; ++i){cin >> a[i];if (a[i] >= 0 && flag == 0)flag = 1;}if(!flag){cout << 0 << " " << a[0] << " " << a[n - 1] << endl;}else {for (i = 0; i < n; ++i){maxnow = a[i], maxyet = 0;for (j = i; j < n; ++j){maxyet = maxyet + a[j];if (maxnow > maxyet)//这里话多余了。但也没改。{//break;//从头到尾走到最后,不要停下}elsemaxnow = maxyet;if (maxend < maxnow)//最终值只记录最大值,并且同时变start,end即可。{maxend = maxnow;start = i, end = j;}}}int sum = 0;for (i = start; i <= end; ++i)sum = sum + a[i];cout << sum << " " << a[start] << " " << a[end] << endl;}return 0;
}
PTA 1007 Maximum Subsequence Sum相关推荐
- PAT甲级1007 Maximum Subsequence Sum :[C++题解]DP,最大子序列和、求最优的区间方案
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: dp题. 这道题糅合了两个知识点: dp求最值(区间之和) 动态求区间方案:区间之和相等的条件下:要求区间左端点最靠前,如果左端点相 ...
- 1007 Maximum Subsequence Sum
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A c ...
- PAT 1007 Maximum Subsequence Sum
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A c ...
- 【PAT甲】1007 Maximum Subsequence Sum (25分),求最大字段和及区间
problem 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N 1 , N 2 , -, N ...
- PAT甲级--1007 Maximum Subsequence Sum (25 分)
题目详情 - 1007 Maximum Subsequence Sum (25 分) (pintia.cn) Given a sequence of K integers { N1, N2, .. ...
- PAT甲级真题-1007 Maximum Subsequence Sum详解优化
1007 Maximum Subsequence Sum 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/9948055 ...
- 1007 Maximum Subsequence Sum
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continu ...
- PAT甲级 -- 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 【测试点5】1007 Maximum Subsequence Sum (25 分)
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a sequence of K integers { N1 , N2 , -, NK }. A co ...
最新文章
- Ubuntu使用QCustomPlot简介
- Intellij IDEA中使用MyBatis-generator 自动生成MyBatis代码
- 一个项目从开工到交付使用需要经历的过程
- Learning Python 009 dict(字典)和 set
- 关于html5的7个传说
- day3----python变量与常量
- 破除“论文至上”!两部委发文规范SCI指标使用
- 2020-2021网络游戏行业人才招聘报告
- python网盘系统_python做系统
- LVS负载均衡DR模式部署
- python粘贴代码到word_python怎么粘贴代码
- JavaScript格式化数字
- codevs 1138
- 计算机系统基础实验 pa1
- 油画的发展过程经历了几个时期?
- 服务器和交换机物理连接_Brocade博科交换机 SAN存储区域网络
- 电大计算机画图程序属性,电大计算机绘图(本)复习大全.doc
- iOS日常开发之常用单词、名词注释
- 在Excel中用VB批量生成二维码
- 指数基金(一):宽基指数简介