Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

初始一个错误点的代码:
#include <iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int a[N];
int sum=,sumMax=-,startPoint=,tempPoint=,endPoint=N-;
//sum 和 sumMax最大数 tempPoint记录临时指针
for(int i=;i<N;i++){
cin>>a[i];
sum+=a[i];
if(sum<){
sum=;
tempPoint=i;
}else if(sum>sumMax){
sumMax=sum;
startPoint=tempPoint+;
endPoint=i;
}
}
if(sumMax<)sumMax=;//应该注意如果没有一个为0,那么应该输出0,首项,末项
cout<<sumMax<<" "<<a[startPoint]<<" "<<a[endPoint];
return ;
}

验证了很久,发现是第i个有错,一定要注意,tempPoint不能在startPoint处+1,理由:首次为0会计数为1,而在sum<0的条件下去计数,则会,首次小于0,将startPoint进行重置为后一个数字,这个错误提醒自己,每个程式都要进行测试用例的猜测。

#include <iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int a[N];
int sum=,sumMax=-,startPoint=,tempPoint=,endPoint=N-;
//sum 和 sumMax最大数 tempPoint记录临时指针
for(int i=;i<N;i++){
cin>>a[i];
sum+=a[i];
if(sum<){
sum=;
tempPoint=i+;
}else if(sum>sumMax){
sumMax=sum;
startPoint=tempPoint;
endPoint=i;
}
}
if(sumMax<)sumMax=;//应该注意如果没有一个为0,那么应该输出0,首项,末项
cout<<sumMax<<" "<<a[startPoint]<<" "<<a[endPoint];
return ;
}
 

数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)的更多相关文章

  1. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  2. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  3. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  4. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  5. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

  6. 【PAT甲级】1007 Maximum Subsequence Sum (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

  7. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  9. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

随机推荐

  1. 一款新的好用的SSH工具——FinalShell,比XShell更牛逼~

    FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求.特色功能:免费海外服务器远程桌面加速,ssh加速,双边tcp加速,内网穿 ...

  2. java 强弱软虚 四种引用,以及用到的场景

    1.利用软引用和弱引用解决OOM问题:用一个HashMap来保存图片的路径和相应图片对象关联的软引用之间的映射关系,在内存不足时,JVM会自动回收这些缓存图片对象所占用的空间,从而有效地避免了OOM的 ...

  3. jieba(结巴)常用方法

    python jieba库的基本使用   第一步:先安装jieba库 输入命令:pip install jieba jieba库常用函数: jieba库分词的三种模式: 1.精准模式:把文本精准地分开 ...

  4. 如何将post请求转换成put和delete请求

    <form:form action="${pageContext.request.contextPath}/emp" method="POST" mode ...

  5. ubuntu的无线网无法连上

    自己的笔记本可以连上wireless,但是实验室的台式机无法连上. 有无线显示,就是无法连上. 后来把连在机箱上的网线拔了以后可以连无线了.如果有网线连接,系统优先会选择有线的上网.

  6. JavaScript中this的一些坑

    我们经常在回调函数里面会遇到一些坑: var obj = { name: 'qiutc', foo: function() { console.log(this); }, foo2: function ...

  7. Vue中的model

    v-model语法糖: model: 默认情况下,一个组件上的 v-model 会把 value 用作 prop 且把 input 用作 event, 但是一些输入类型比如单选框和复选框按钮可能想使用 ...

  8. Android下Native的so编译:使用cmakelists.txt

    android studio的CMakeLists.txt写好后,在Studio中编译没问题,在命令编译时则需要配置很多参数才能成功 示例CMakeLists.txt文件: cmake_minimum ...

  9. python每日一练:0011题

    第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights. 北京 程序员 公 ...

  10. 学习kettle遇到的问题

    一. 解决mysql连接缺少驱动问题:http://www.mamicode.com/info-detail-1724584.html 1.下载驱动 https://dev.mysql.com/dow ...