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. [LeetCode]-009-Palindrome_Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  2. tomcat+nginx负载均衡

    一.       工具 nginx-1.8.0 apache-tomcat-6.0.33 二.    目标 实现高性能负载均衡的Tomcat集群: 三.    步骤 1.首先下载Nginx,要下载稳定 ...

  3. PTA编程总结三

    7-1 抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块 ...

  4. Window下PHP环境配置使用Redis总结

    什么是Redis? Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的API.它和Memcached类似,它支持存储的 ...

  5. [NN] Guided Backpropgation 可视化

    Pytorch Guided Backpropgation Intro guided backpropgation通过修改RELU的梯度反传,使得小于0的部分不反传,只传播大于0的部分,这样到第一个c ...

  6. MySQL表碎片清理

    MySQL大表清理 生产环境data库业务表base_data大小:500G,data_free:31G mysql> SELECT table_schema,table_name,data_f ...

  7. vim字体设置

    经过多方试验,在win下设置vim的大小终于成功了,备份如下, 现在把gvim7.1更改字体的方法记录如下,一段时间后,可能会失效,对他人造成困扰吧?!^_^   在_vimrc中写:set guif ...

  8. Linux_Samba详解

    目录 目录 Samba Server Parameter Configuration file explain Setup the Samba Server Access the samba shar ...

  9. Burp Suite批量网页操作

    1.打开md5解密网站,并输入“21232F297A57A5A743894A0E4A801FC3”,不要点击[Decrypt It!] 1.启动Burp Suite,并设置浏览器代理 3.点击[Dec ...

  10. ftp搭建mysql服务器

    一.将mysql放入FTP服务器中1.安装FTP    yum install -y vsftpd2.准备ftp主目录    mkdir /var/ftp/mysql57/3.官网下载yum仓库的包. ...