Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1329

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1329

Head or Tail

 

Maratona de Programacao da SBC 2004, Warm-Up Brazil

Timelimit: 1

John and Mary have been friends since nursery school. Since then, they have shared a playful routine: every time they meet, they play Head or Tail with a coin, and whoever wins has the priviledge of deciding what they are going to play during the day. Mary always choose Head, and John always choose Tail.

Nowadays they are in college, but continue being truly good friends. Whenever they meet, they still play Head and Tail, and the winner decides which film to watch, or which restaurant to have dinner together, and so on.

Yesterday Mary confided to John that she has being keeping a record of the results of every play since they started, in nursery school. It came as a surprise to John! But since John is studying Computer Science, he decided it was a good opportunity to show Mary his skills in programming, by writing a program to determine the number of times each of them won the game over the years.

 

Input

 

The input contains several test cases. The first line of a test case contains a single integer N indicating the number of games played (1 ≤ N ≤ 10000). The following line contains N integers Ri, separated by space, describing the list of results. If Ri = 0 it means Mary won the ith game, if Ri = 1 it means John won the ith game (1 ≤ iN). The end of input is indicated by N = 0.

 

Output

 

For each test case in the input your program should output a line containing the sentence "Mary won X times and John won Y times", where 0 ≤ X and 0 ≤ Y.

 

 

 

Sample Input Sample Output

5
0 0 1 0 1
6
0 0 0 0 0 1
0

Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdbool.h>

int main (void)
{

	unsigned char numero;
	unsigned short mary = 0, john = 0, casos;

	while (true)
	{

		scanf("%hu", &casos);

		if (casos == 0)
			break;

		mary = john = 0;
		while (casos--)
		{

			scanf("%hhd", &numero);

			if (numero == 0)
				mary++;
			else
				john++;
		}

		printf("Mary won %hu times and John won %hu times\n", mary, john);

	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
0 0 1 0 1
6
0 0 0 0 0 1
0

Output

x
+
cmd
Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main(void) {
    int q, n, m, j, i;
    while (cin >> q && q != 0) {
        m = j = 0;
        for (i = 0; i  <  q; ++i) {
            cin >> n;
            if (n) ++j;
            else ++m;
        }
        cout << "Mary won " << m << " times and John won " << j << " times" << endl;
    }
    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
5
0 0 1 0 1
6
0 0 0 0 0 1
0

Output

x
+
cmd
Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times

#3 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
      Scanner entrada = new Scanner(System.in);
        int n = entrada.nextInt();
        while(n !=0){
            int m = 0, j =0;
            for(int i = 0; i  <  n; i++){
                int p = entrada.nextInt();
                if(p == 0)
                   m++;
                else
                   j++;
            }
            System.out.println("Mary won "+m+" times and John won "+j+" times");
            n = entrada.nextInt();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
0 0 1 0 1
6
0 0 0 0 0 1
0

Output

x
+
cmd
Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")

function main() {
	const responses = []

	for (let index = 0; index  <  input.length; index += 2) {
		const numGames = input[index]

		if (numGames == "0") break

		const scoreboard = input[index + 1].split(" ").reduce((wons, value) => {
			if (value === "0") wons.mary += 1
			else if (value === "1") wons.john += 1

			return wons
		}, { mary: 0, john: 0 })

		responses.push(
			`Mary won ${scoreboard.mary} times and John won ${scoreboard.john} times`
		)
	}

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
0 0 1 0 1
6
0 0 0 0 0 1
0

Output

x
+
cmd
Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times

#5 Code Example with Python Programming

Code - Python Programming


while True:
    n = int(input())
    if n == 0: break
    lis = str(input())
    m = lis.count('0')
    j = n - m
    print('Mary won {} times and John won {} times'.format(m, j))

Copy The Code & Try With Live Editor

Input

x
+
cmd
5
0 0 1 0 1
6
0 0 0 0 0 1
0

Output

x
+
cmd
Mary won 3 times and John won 2 times
Mary won 5 times and John won 1 times
Advertisements

Demonstration


Previous
#1327 Beecrowd Online Judge Solution 1327 Drop Out Solution in C, C++, Java, Js and Python
Next
#1337 Beecrowd Online Judge Solution 1337 King’s Poker Solution in C, C++, Java, Js and Python