【数据结构练习】1.最大子列问题
来源:互联网 发布:河南省公安厅网络 编辑:程序博客网 时间:2024/05/22 07:58
后面会不定期把通过的PTA源码搬到博客上,记录自己学习数据结构的历程,我是数据结构爱好者~一开始一直考虑负数的情况想直接写进子函数里,后来干脆直接写在main里作为特例,总之写的有点乱。
题目来自 浙江大学数据结构MOOC PTA
01-复杂度2 Maximum Subsequence Sum(25 分)
Given a sequence of K integer { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …,Nj} where 1≤i≤j≤K. 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 (≤10000). 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<stdio.h>//#define n 10int First,Last;int MaxSubseqSum4( int A[], int N );int main(){ int n; scanf("%d",&n);// int b,c,d,e,f,g; int A[n]; int i,N; int r;// printf("the number is : %d\n", n); for ( i = 0 ; i < n ; i++ ) { scanf ("%d",&A[i]); }// scanf("%d %d %d %d %d %d",&b,&c,&d,&e,&f,&g);// printf("the number is : %d %d %d %d %d %d\n", A[0],A[1],A[2],A[3],A[4],A[5]); r=MaxSubseqSum4( A, n ); //后续处理 if( r == 0 ) { int index = -1; for(int i = 0; i < n; ++i){ if(A[i] == 0) index = i; } if(index != -1){ First = 0 ; Last = 0 ; } else{ First = A[0]; Last = A[n-1]; } }// else{ // int m = MaxSubseqSum4; // int p = Last; // while(m > 0){ // First = m; // m -= A[p--]; // } //} printf("%d %d %d\n", r,First,Last);}int MaxSubseqSum4( int A[], int N ){ int ThisSum,MaxSum; int i; int tempFirst,tempLast; ThisSum = MaxSum = 0; First = Last = tempFirst = tempLast = A[0]; for( i = 0; i < N ; i++ ) { ThisSum += A[i]; if( ThisSum >= 0 ) { tempLast = A[i]; } else if( ThisSum < 0 ) { tempFirst = A[i+1]; ThisSum = 0; } if( ThisSum > MaxSum ) { MaxSum = ThisSum; Last = tempLast; First = tempFirst; } } return MaxSum;}
- 【数据结构练习】1.最大子列问题
- MOOC 数据结构 最大子列和问题
- 数据结构(最大子列和问题)
- 数据结构学习-最大子列和问题
- 数据结构--最大子列和
- 数据结构 ---- 最大子列和
- 最大子列和问题【数据结构测试1.1】
- 数据结构 PAT 01-复杂度1 最大子列和问题
- 数据结构与算法--求最大子列和问题
- PAT数据结构_01-复杂度1 最大子列和问题
- 数据结构学习笔记-Day1-最大子列和问题
- 求最大子列和问题(浙江大学数据结构)
- [MOOC-陈越何钦铭数据结构]01-1. 最大子列和问题(20)
- PAT 数据结构 01-复杂度1. 最大子列和问题(20)
- 01-1. 最大子列和问题
- 01-1. 最大子列和问题
- 复杂度1. 最大子列和问题
- 最大子列和问题(同时输出有最大和的子列的首尾元素)【数据结构测试1.2】
- linux安装zookeeper单机版
- LeetCode-57-Insert Interval 水
- 关于Jedis连接Linux上的redis出现 DENIED Redis is running in protected mode问题的解决方案
- python总结(四):类装饰器与方法的动态添加
- 线性表的两种实现
- 【数据结构练习】1.最大子列问题
- vim配置
- Round四舍五入用法举例
- Android应用opencv编译出错
- CS231N-5-Convolutional Neural Networks
- Oracle PL/SQL进阶编程(第一弹:子程序结构)
- VS2013用MFC操作64位access 2013
- 互联网创造价值吗?
- zabbix触发器介绍