24h購物| | PChome| 登入
2012-05-30 06:08:52| 人氣1,856| 回應0 | 上一篇 | 下一篇

[UVA] 10684 - The jackpot

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D - The jackpot

Time Limit: 1 second

Memory Limit: 1 Mb

Background

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.

The Problem

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

5
12 -4
-10 4
9
3
-2 -1 -2
0

Sample output

The maximum winning streak is 13.
Losing streak.

我個人覺得是不需要甚麼 DP 就是了

#include <stdio.h>

int main() {
    int n;
    while(scanf("%d", &n) == 1 && n) {
        int ans = 0, x, tmp = 0;
        while(n--) {
            scanf("%d", &x);
            tmp += x;
            if(tmp < 0)
                tmp = 0;
            if(tmp > ans)
                ans = tmp;
        }
        if(ans)
            printf("The maximum winning streak is %d.\n", ans);
        else
            printf("Losing streak.\n");
    }
    return 0;
}

台長: Morris
人氣(1,856) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10700 - Camel trading
此分類上一篇:[UVA][BFS] 352 - The Seasonal War

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文