【数据结构练习】1.最大子列问题

来源:互联网 发布:河南省公安厅网络 编辑:程序博客网 时间:2024/05/22 07:58

后面会不定期把通过的PTA源码搬到博客上,记录自己学习数据结构的历程,我是数据结构爱好者~一开始一直考虑负数的情况想直接写进子函数里,后来干脆直接写在main里作为特例,总之写的有点乱。

题目来自 浙江大学数据结构MOOC PTA

01-复杂度2 Maximum Subsequence Sum(25 分)
Given a sequence of K integer { N​1, N2, …, NK​​ }. A continuous subsequence is defined to be { Ni, Ni+1, …,N​j} 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;}
原创粉丝点击