【UVa】10684 - The jackpot

该博客讲述了如何帮助Manuel通过编程分析赌博收益,找出连续赢钱的模式,以制定赢得策略。程序需要找出一系列赌注中能获得的最大可能收益。如果序列中没有可能赢钱,则输出‘Losing streak.’。

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

Problem here

Problem

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).

Input

The 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.

Output

For 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

6
-99 10 -9 10 -5 4
3
-999 100 -9
5
12 -4
-10 4
9
3
-2 -1 -2
0

Sample Output

The maximum winning streak is 11.
The maximum winning streak is 100.
The maximum winning streak is 13.
Losing streak.

Solution

dp[i] 是以i為結尾的最大收入
取「dp[i-1]加上第i次賭錢的結果」與「只計第i次賭錢的結果」較大的一個
所以有dp[i] = max(dp[i-1] + data[i], data[i]);
前題是要先設dp[0] = data[0](沒有dp[0-1] ,且以第一個數作為結尾的答案只有一個嘛\\就是第一個數嘛orz)

#include <iostream>
#include <vector>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        if(n == 0)
            break;

        int dp[100001];
        vector<int> data;
        for(int i = 0; i < n; i++){
            int input;
            cin >> input;
            data.push_back(input);
        }
        dp[0] = data[0];

        for(int i = 1; i < n; i++){
            dp[i] = max(dp[i-1] + data[i], data[i]);
        }

        int ans = -100000;
        for(int i = 0; i < n; i++){
            if(ans < dp[i])
                ans = dp[i];
        }
        if(ans <= 0)
            cout << "Losing streak." << endl;
        else
            cout << "The maximum winning streak is " << ans << "." << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值