Solution of 10684- The jckpot

Problem description:
source: https://uva.onlinejudge.org/external/106/10684.html

As Manuel wants to get rich faster 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

5
12 -4 
-3 -4
9
0

Sample output

The maximum winning streak is 18.

Solution:
#include<stdio.h>
using namespace std;
static int inputSequence[10002];
int main() {
    static int n, i, sum, maxGain;

    while((scanf("%d", &n))== 1) {
        if(n == 0) {
            break;
        }
        sum =0;
        maxGain = 0;
        for(i=0; i < n; i++) {
            scanf("%d", &inputSequence[i]);
            sum += inputSequence[i];
            if(sum < 0) {
                sum = 0;
            }
            if(sum > maxGain) {
                maxGain = sum;
            }
        }
        if(maxGain > 0) {
            printf("The maximum winning streak is %d.\n", maxGain);
        } else {
            printf("Losing streak.\n");
        }
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience