1007.Maximum Subsequence Sum (25)最大的子序列和(另解)

最大的子序列和(1007.Maximum Subsequence Sum (25) | Programming Ability Test https://www.patest.cn/contests/pat-a-practise/1007)

有一个包含K个整数{ N1, N2,..., NK }的序列。一个连续的子序列被定义为 { Ni, Ni+1, ..., Nj }(1<= i <= j <= K)。最大的子序列是指在连续的子序列中元素和最大的那个。例如,一个序列{ -2, 11, -4, 13, -5, -2 },它的最大子序列是{ 11, -4, 13},最大和为20。

现在你需要找出最大和,以及最大子序列的第一个和最后一个序号。

输入说明:

每个输入文件包含一个测试实例。每个实例包含两行。第一行包含一个正整数K (<= 10000)。

第二行包含K个数,用空格分隔开。

输出说明:

对于每个测试实例,在一行中输出最大和,同时输出最大子序列的第一个和最后一个序号。

数字必须用一个空格分隔开,但是在一行末尾不能有多余的空格。如果最大子序列不是唯一的,则输出最小的指数i和j(像样例中那样)。如果K个数都是负的,则最大和被定义为0,你应该输出整个序列的第一个和最后一个数。

Sample Input:

10

-10 1 2 3 4 -5 -233 7 -21

Sample Output:

10 1 4

#include<stdio.h>
#define N 10005
int main()
{
	int k,n[N],i,j;
	long sum=-1,sumT[N]={0};//初始化求和项 
	long st,sti,stj;       //定义最大子序列的第一个和最后一个序号sti,stj
	scanf("%d",&k);
	for(i=0;i<k;i++){
		scanf("%d",&n[i]);//输入 
	}
	for(i=0;i<k;i++)
	{
		if(i==0)
			sumT[0]=n[0];//第1项赋值 
		else
			sumT[i]=sumT[i-1]+n[i];//前i项之和 
	}
	for(i=0;i<k;++i)
		for(j=i;j<k;++j)
		{
			if(i==0)//第1项开始,是正整数 
			{			
				if(sum<sumT[j])
				{
					sum=sumT[j];//把前j项之和赋给sum
					sti=0;     //把0赋给最大子序列的第一个序号sti
					stj=j;    //把j赋给最大子序列的第最后一个序号stj
				}
			}
			else//第i项开始,是正整数
			{
				if(sum<sumT[j]-sumT[i-1])
				{
					sum=sumT[j]-sumT[i-1];
					sti=i;
					stj=j;
				}
			}
		}
	if(sum<0)//按要求输出 
		printf("%d %d %d\n",0,n[0],n[k-1]);
	else
		printf("%ld %d %d\n",sum,n[sti],n[stj]);
	
	return 0;
}
//另解
#include <stdio.h>

int main(void)
{
	int thisSum,maxSum;
	int leftNumber,rightNumber,temporaryLeftNumber;
	int k;
	scanf("%d",&k);	//读取整数个数 
	int array[k];
	for(int i = 0;i < k;i++)	//读取k个整数存入数组 
		scanf("%d",&array[i]);
	
	thisSum = maxSum = 0;	//将thisSum 和 maxSum 设为0 
	leftNumber = array[0];       //为了处理全部小于0时输出首尾数字的情况,
								//先将数组头尾的数字赋给记录子列左右数字的变量 
	rightNumber = array[k-1];
	temporaryLeftNumber = array[0]; //记录左边数字的暂定值 
	for(int i = 0;i < k;i++)
	{
		thisSum += array[i];	//向右累加
		if(thisSum > maxSum)
		{
			leftNumber = temporaryLeftNumber;
			maxSum = thisSum;	//发现更大和则更新当前结果
			rightNumber = array[i];			//如果有更大和,那么不断更新右边的数字 
		}
			
		if(thisSum < 0)	//如果当前子列和为负
		{
			thisSum = 0; 		//则不可能使后面的部分和增大,抛弃之 
			if(i + 1 < k)
				temporaryLeftNumber = array[i + 1];
		}
		
		if(array[i] == 0 && maxSum == 0 )	//如果A[i]为0的且最大子列和也为0,则将左右数字都设为0 
		{
			leftNumber = 0;
			rightNumber = 0;
		}
	}
	
	printf("%d %d %d",maxSum,leftNumber,rightNumber);	//输出答案 
 }  

题目原文如下:

1007. Maximum Subsequence Sum (25)

Given a sequenceof K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be {Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence isthe 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 aresupposed to find the largest sum, together with the first and the last numbersof the maximum subsequence.

InputSpecification:

Each input filecontains one test case. Each case occupies two lines. The first line contains apositive integer K (<= 10000). The second line contains K numbers, separatedby a space.

OutputSpecification:

For each testcase, output in one line the largest sum, together with the first and the lastnumbers 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 maximumsubsequence is not unique, output the one with the smallest indices i and j (asshown by the sample case). If all the K numbers are negative, then its maximumsum is defined to be 0, and you are supposed to output the first and the lastnumbers of the whole sequence.

SampleInput:

10

-10 1 2 3 4 -5 -23 3 7 -21

SampleOutput:

10 1 4



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值