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 ;
}
 

最新文章

  1. oneM2M启动Release 3标准化,华为引领物联网技术布局
  2. SAP HR宏 rp-provide-from-last
  3. SQLite打开提示database disk image is malformed
  4. HTML你应该知道的三大基本元素
  5. HDU2056JAVA
  6. SQL中ISNULL的使用
  7. Linux企业级项目实践之网络爬虫(27)——多路IO复用
  8. PowerDesigner反projectM连接ySql没有mySql odbc驱动器
  9. 【android】WebView缓存数据收集
  10. Sublime text3插件安装方法
  11. ZOJ 3228 Searching the String(AC自动机)
  12. MATLAB三点确定圆
  13. Nagios故障 CHECK_NRPE: Socket timeout after 10 seconds.
  14. drf3 Serializers 序列化组件
  15. SSL/TLS Server supports TLSv1.0
  16. 【转】 python中 * 的用法
  17. 恶意代码分析实战-确认EXE什么时候编译的
  18. NSZombie 详解 -僵尸对象
  19. (算法)Trapping Rain Water I
  20. 高性能WEB开发:DOM编程

热门文章

  1. Docker push image to Docker hub
  2. Vue 中 使用v-show
  3. Yahoo 军规(部分)
  4. springBoot+springSecurity 数据库动态管理用户、角色、权限(二)
  5. metrics+spring+influxdb
  6. Xcode10报错 library not found for -lstdc++ 问题解决
  7. DNS 搜索 - dig 命令
  8. 用U盘完成win10系统的安装
  9. 一种局部二值化算法:Sauvola算法
  10. 【java】的传值方式