数据结构课后练习题(练习一)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 }. ...
随机推荐
- c++ 指针类型转换
1.数据类型转换(static_cast) //数据类型转换printf("%d\n", static_cast<int>(10.2)); 2.指针类型转换(reint ...
- C++ STL unordered_map
容器unordered_map<key type,value tyep>m; 迭代器unordered_map<key type,value tyep>::iterator i ...
- 基于jquery的bootstrap在线文本编辑器插件Summernote 简单强大
Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...
- 我们建了一个 Golang 硬核技术交流群(内含视频福利)
目录 目录 Golang 是什么? 我们为什么选择 Golang? Golang 是云时代的宠儿! 我们搞了一场 Golang 入门直播 Golang 是什么? Golang 是谷歌 2009 年发布 ...
- oracle expdp/impdp/可传输表空间
oracle expdp/impdp/可传输表空间/及一些参数 Oracle data pump 导出操作能够将表.索引.约束.权限.PLSQL包.同义词等对象从数据库导出,并将它们保存在一种非文本格 ...
- 6.824 Lab 3: Fault-tolerant Key/Value Service 3A
6.824 Lab 3: Fault-tolerant Key/Value Service Due Part A: Mar 13 23:59 Due Part B: Apr 10 23:59 Intr ...
- python+ selenium&APPium自动化 page Object 设计模式
题记: 之前公司项目比较稳定, 在进行了系统测试,想用自动化测试进行冒烟测试,或者对主要功能进行测试, 因此用到了PO模式 因此做个记录: Page Object Page Object模式是使用Se ...
- 【MM系列】SAP MM模块-如何修改物料的移动平均价
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-如何修改物料的移动 ...
- python+selenium控制浏览器窗口(刷新、前进、后退、退出浏览器)
调用说明: driver.属性值 变量说明: 1.driver.current_url:用于获得当前页面的URL 2.driver.title:用于获取当前页面的标题 3.driver.page_so ...
- 八、Zabbix-应用集、监控项
一.应用集 1.应用集一般配合监控项使用,它相当于多个同类型的监控项的分类目录 2.添加应用集 (1)配置—>模板—>需要调整的模板—>应用集 (2)添加应用集 (3) (4)查看应 ...