Topic from the "Data structure problem" of NetEase cloud Classroom
Given a sequence of K integers {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 have the largest sum of its elements. For example, given sequence {-2, one, -4, -5, 2}, its maximum subsequence are {One, -4, all} with the largest sum bei ng 20.
Now is supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains the one test case. Each case occupies the 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 on one line the largest sum, together with the first and the last numbers of the maximum Subseq Uence. The numbers must is separated by one space, but there must is no extra space at the end of a line. In case the maximum subsequence are not unique, output the one with the smallest indices I and j (as shown by the Samp Le case). If all the K numbers was negative, then it maximum sum is defined to being 0, and you were supposed to output the first and T He last numbers of the whole sequence. Sample Input:
Ten
-10 1 2 3 4-5-23 3 7-21
Sample Output:
10 1 4
#include <stdio.h>
int main (void)
{
int K, I, sum = 0, max = 0, begin, end, TMP;
printf ("Please input K:");
scanf ("%d", &k);
int A;
printf ("Please enter into K numbers, split by space:\n");
for (i=0; i<k; ++i)
{
scanf ("%d", &a);
if (i = = 0)
tmp = A;
if (Sum < 0)
{
sum = A;
TMP = A;
}
else
{
sum + = A;
}
if (Sum > Max)
{
begin = tmp;
end = A;
max = sum;
}
}
if (Max < 0)
printf ("The Maximum subsequence sum:0, begin with%d, end with%d", begin, end);
else
printf ("The Maximum subsequence Sum:%d, begin with%d, end with%d", Max, begin, end);
return 0;
}