第一周编程作业: Maximum Subsequence Sum
2018-03-13 14:57
369 查看
01-复杂度2 Maximum Subsequence Sum(25 point(s)) 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≤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
程序实现如下:
integers { N 1 , N 2 , …, N K }. A continuous
subsequence is defined to be { N i , N i+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> #include <stdlib.h> //void print_arr(int n, int * arr); int main(void) { int n = 0; scanf("%d", &n); int * arr = (int *)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); getchar(); } //int arr[5]={1,2,-8,2,1}; //n=5; //n=10; //int arr[10]={10,-2,5,5,-1,-100,100,-2,1,2}; //int arr[10]={-1,-2,-3,-4,-8,-3,-8,-9,-7,-11};//all the number are negative //int arr[10]={-5,-2,8,-1,9,1,-10,0,0,0};//the sum have a negative number //int arr[10]={};//all the number is zero //int arr[10]={0,0,10,-1,-1,5,-1,-1,-1,-100}; //int arr[10]={-2,-5,1,50,-1,-1,-2,-300,-4,-8}; //int arr[10]={100,-8,-8,1,-8,-1000,1,2,0,0}; //int arr[10]={-1,-2,-3,-5,-1,0,-1,-1,-1,-2}; int next_sum, current_sum; next_sum = current_sum = 0; int start=0, end=n-1;//final result int start1=0,end1=n-1;//maybe the final result int flag=0; int tempSum=0; int s=0; int j=0; int k=1; int t=0; // int flag2 = 0; //int start_temp=0; for (int i = 0; i < n; i++)//check all the numbers { current_sum += arr[i]; if (flag == 0) { if (arr[i] > 0|| arr[i]==0)//find a posstive number { start1 = i;//mark it t=start1; //start=start1; //printf("**********%d\n",start1); if(arr[i]==0) { k++; } flag = 1; } else//can't find a suitable number { current_sum=0; //printf("positive!!\n"); k++; continue; } } if ( current_sum < 0) { s=current_sum; tempSum=current_sum-arr[i];//maybe the maxsum start1=i+1; j=i-1; while(arr[j]<0&&s==tempSum) { s=s-arr[j]; end=j-1; //start=start1; j--; // printf("*******%d\n",end); } if(tempSum<current_sum)//when it's larger than before { start=start1; end=end1; } current_sum = 0; flag=0; } else if (current_sum > next_sum) { next_sum = current_sum; end1 = i; // printf("*******end1=%d\n",end1); if(tempSum<current_sum)//when it's larger than before { start=start1; end=end1; } } } if(k==n) { start=t; end=t; } // printf("less than max=%d\n",tempSum); // printf("sum=%d %d %d\n", next_sum,start,end); // for(int i=start;i<stop+1;i++) // { // next_sum=next_sum+arr[i]; // } printf("%d %d %d", next_sum, arr[start], arr[end]); //print_arr(n, arr ); return 0; }
相关文章推荐
- [PTA MOOC] Maximum Subsequence Sum(25 分)(也是浙大研究生机试的题)
- Maximum Subsequence Sum
- Maximum Subsequence Sum Problem
- PAT (Advanced Level) Practise 1007 Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum(接上篇)
- PAT Maximum subsequence sum (Python)
- 最大子列和CT 01-复杂度2 Maximum Subsequence Sum
- Maximum Subsequence Sum
- C++ 01-复杂度2 Maximum Subsequence Sum
- Maximum Subsequence Sum【PAT 1007题】---简单的动态规划
- PAT_1007: Maximum Subsequence Sum
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- Maximum Subsequence Sum
- 01-复杂度2 Maximum Subsequence Sum
- Maximum Subsequence Sum (25)
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
- The Maximum Subsequence Sum Problem
- ●CodeForces 280D k-Maximum Subsequence Sum
- Maximum Subsequence Sum