As Manuel wants to get rich fast and without too much work, he decided to make a career in gambling. Initially, he plans to study the gains and losses of players, so that, he can identify patterns of consecutive wins and elaborate a win-win strategy. But Manuel, as smart as he thinks he is, does not know how to program computers. So he hired you to write programs that will assist him in elaborating his strategy.
Your first task is to write a program that identifies the maximum possible gain out of a sequence of bets. A bet is an amount of money and is either winning (and this is recorded as a positive value), or losing (and this is recorded as a negative value).
InputThe input set consists of a positive number N <= 10000 , that gives the length of the sequence, followed by N integers. Each bet is an integer greater than 0 and less than 1000.
The input is terminated with N = 0.
OutputFor each given input set, the output will echo a line with the corresponding solution. If the sequence shows no possibility to win money, then the output is the message "Losing streak."
Sample Input
5 12 -4 -10 4 9 3 -2 -1 -2 0
Sample Output
The maximum winning streak is 13.Losing streak.最大字段和问题,下面的代码用dp做:
用s[i]表示以a[i]结尾的最大字段,状态转移方程:s[i]=max{0,s[i-1]}+a[i];
#include<stdio.h>
#include<string.h>
#include <limits.h>
int main()
{
int n;
while(scanf("%d",&n),n)
{
int i,j;
int a[n+5];
int s[n+5];
memset(s,0,sizeof(s));
for(i=1;i<=n;i++)scanf("%d",&a[i]);
a[0]=0;
s[0]=0;
int m=0;
for(i=1;i<=n;i++)
{
if(s[i-1]>0)
s[i]=s[i-1]+a[i];
else s[i]=a[i];
if(m<s[i])m=s[i];
}
if(m>0)
printf("The maximum winning streak is %d.\n",m);
else printf("Losing streak.\n");
}
}
本文探讨如何通过编程辅助制定赌博策略以实现快速致富,包括识别盈利模式、设计赢赢策略。通过输入序列的赌注结果,输出可能的最大赢利或警告连续亏损。
2247

被折叠的 条评论
为什么被折叠?



