数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
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 分)的更多相关文章
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...
- 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 ...
- 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 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
随机推荐
- 解析获得的网页数据(XML文件或JSON文件)
1.解析XML:使用Pull方式. 需要导入jar包:xmlpull-xpp3-1.1.4c.jar //Pull解析XML文件 private void parseXMLWithPull(Strin ...
- 子系统kali安装桌面
理论上讲,所有Win10的Linux子系统都可以通过Windows10本机远程桌面和Xming的方法来安装使用图形化界面,笔者目前只接触了Debian系的Linux系统,故以Debian GNU/Li ...
- 基于原生XMLHttpRequest封装
用了一段时间的Ajax,感觉有很多的不足之处,于是就封装原生了 XMLHttpRequest . 废话不多说,直接上代码. var http = function () { 'use strict'; ...
- JSP———数据交互【1】
JSP的内置对象 不用声明就可以在JSP页面中使用 request对象 内置对象 request 封装了用户提交的信息,主要用于处理客户端请求 <FORM action="tom.js ...
- Jmeter中各种函数
${__functionName(var1,var2,var3)} 无参数时,可以直接写成${__functionName} Tips: 如果参数包含逗号,那么一定要使用 \ 来转义,否则 JMete ...
- springBoot(2) HelloWorld
首先 下载一个 Spring Boot环境. 下载地址:https://spring.io/tools3/sts/all 打开STS.exe 一,新建项目 然后在空白处新建: 1.New→Other→ ...
- jeecg项目将workbook 的Excel流添加到zip压缩包里导出
1.直接献出代码 Map<String,List<ConfidentialInformation>> typeMap = new HashMap<>(); try ...
- 从零构建vue项目(一)--搭建node环境,拉取项目模板
本文是基于vuecli2搭建的项目. 1. 下载安装nodejs 地址:https://nodejs.org/en/download/ 选择安装版windows .msi, 不要选择压缩版 下 ...
- 所遇Oracle错误代码
ORA-00905: 缺失关键字 少了空格或关键字写错 ORA-00922: 选项缺失或无效 错误原因:一般是语句的语法有问题.比如命名不对,关键字写错等等.对于非标准的命名,一般采用双引号来创建 ...
- python 并发编程 多进程 队列目录
python 并发编程 多进程 队列 python 并发编程 多进程 生产者消费者模型介绍 python 并发编程 多进程 生产者消费者模型总结 python 并发编程 多进程 JoinableQue ...