博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浙江大学数据结构 01-复杂度2 Maximum Subsequence Sum (25 分)
阅读量:4621 次
发布时间:2019-06-09

本文共 3093 字,大约阅读时间需要 10 分钟。

Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } 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 Knumbers, 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
1 #include
2 using namespace std; 3 4 int main() 5 { 6 int num,number; 7 int sum = 0,max = 0; //max为最大和 sum为累加值,用来遍历序列 8 cin>>num; 9 int index1,index2,index3,index4; //index1为第一段最大和的第一个数,index2为最大和的最后一个数 index3用来记录非第一段的最大和的第一个数 index4用来记录序列的第一个数。10 bool flag = true; //防止最大和序列的第一个数被改变11 bool flag1 = true; //用来标记每一段的开头12 bool flag2 = true; //记录第一个数13 while (num--) {14 cin>>number;15 if(flag2) {16 flag2 = false;17 index4 = number;18 }19 sum += number;20 if(flag1 && number > 0) {21 index3 = number;22 flag1 = false;23 }24 if (sum > max) {25 max = sum;26 if (flag) {27 index1 = number;28 flag = false;29 }30 if (!flag)31 index1 = index3;32 index2 = number;33 }34 if (sum < 0) {35 if(!flag1)36 flag1 = true;37 sum = 0;38 }39 }40 if(max)41 cout<
<<" "<
<<" "<
<

测试点5没过 提示负数和0

看了别人的答案用了数组来保存数据,不用数组保存确实会麻烦

1 #include
2 int arr[10010]; 3 int main(){ 4 int n, ThisSum=0, MaxSum=-1, Minindex=0, Maxindex=0, Tempindex=0; 5 scanf("%d", &n); 6 for(int i=0; i
MaxSum){10 MaxSum=ThisSum;11 Maxindex=i;12 Minindex=Tempindex;13 }14 if(ThisSum<0){15 ThisSum=0;16 Tempindex=i+1;17 }18 }19 if(MaxSum<0)printf("0 %d %d", arr[0], arr[n-1]);20 else{printf("%d %d %d", MaxSum, arr[Minindex], arr[Maxindex]);}21 return 0;22 }

 

 

转载于:https://www.cnblogs.com/Zw1999/p/10850217.html

你可能感兴趣的文章
关于mysql中实现replace的sql语句
查看>>
oracle 字符串排序
查看>>
软工实践Alpha冲刺(5/10)
查看>>
[2019/03/17#杭师大ACM]赛后总结(被吊锤记)
查看>>
Java生成不重复的数的方法
查看>>
末学者笔记--keepalived部署
查看>>
Codechef CARDLINE
查看>>
“尸鬼封尽”与面向对象
查看>>
UIPickerView
查看>>
Product→Analyze 分析内存泄露情况
查看>>
黑客与画家:编程是一种艺术创作
查看>>
Python学习笔记整理总结【MySQL】
查看>>
js继承
查看>>
Java线程安全synchronize学习
查看>>
Cookie Session和自定义分页
查看>>
Python sql数据的增删改查简单操作
查看>>
Apache 2.4配置反向代理
查看>>
半路出家的仓库管理员如何成为一个好的前端工程师(二)
查看>>
ArcGIS API for JavaScript 4.2学习笔记[6] goTo()地图动画
查看>>
实验八——函数定义及调用总结
查看>>