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
程序实现如下:
#include
#include
//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]
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?