UVa 10656 Maximum Sum (II) (water ver.)

本文讨论了如何在给定的非负整数序列中找到和最大的子序列,并详细解释了解决此问题的算法步骤。通过实例分析,展示了算法的应用过程,包括输入输出规范及代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

10656 - Maximum Sum (II)

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1597

In a given a sequence of non-negative integers you will have to find such a sub-sequence in it whose summation is maximum.

 

Input

The input file contains several input sets. The description of each set is given below:

 

Each set starts with an integer N (N<1000) that indicates how many numbers are in that set. Each of the next N lines contains a single non-negative integer. All these numbers are less than 10000.

 

Input is terminated by a set where N=0. This set should not be processed.

 

Output

For each set of input produce one line of output. This line contains one or more integers which is are taken from the input sequence and whose summation is maximum. If there is more than one such sub-sequence print the one that has minimum length. If there is more than one sub-sequence of minimum length, output the one that occurs first in the given sequence of numbers. A valid sub-sequence must have a single number in it. Two consecutive numbers in the output are separated by a single space.

 

Sample Input     Output for Sample Input

2

3

4

0                            

3 4                                                            


注意子序列可以不连续(有些题没说连续但是样例是连续的,有些题看上去像是连续的但WA了后才知道是可以不连续的)


完整代码:

/*0.029s*/

#include<cstdio>

int main()
{
	int n, a;
	bool vis;
	while (scanf("%d", &n), n)
	{
		vis = false;
		while (n--)
		{
			scanf("%d", &a);
			if (a)
			{
				if (vis) putchar(' ');
				vis = true;
				printf("%d", a);
			}
		}
		if (!vis) putchar('0');
		putchar(10);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值