Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1218

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

Getline Three - Shoes

 

By Neilor Tonin, URI Brazil

Timelimit: 1

Now that Mangojata solved some problems using getline, she is able to take a step forward. She is about to make a new program to help her sister, Overlaine. Overlaine is a seller of shoes and by a neglect, mixed all shoes of each box. Now she wants to inform any number N and count how many shoes of this size (N) exists inside a specific box. The problem is that Overlaine have no any idea how many shoes are in each box. The only thing we know is that each footwear may be numbered 20-44, and may be male or female.

 

Input

 

The input contains several test cases and ends with EOF (End of File). Each test case is composed by two lines. The first line contains a number (20 ≤ ≤ 44)  of a shoe informed by Oveline. The second line contains the number of each pair that are inside the box, followed by M or N, indicating if this shoes are Masculine or Feminine.

 

Output

 

For each test case print four lines, like given example. In the first line you must print the message “Caso n:”, where n is the number of the test case. In the second line you must print the message “Pares Iguais:” followed by how many pairs of shoes have the same size of that wich Overlaine wants to find. Follow two lines with the respective among of Feminine (F) and Masculine (M) shoes, with corresponding message.

Print a blank line between the outputs for two consecutive test cases.

 

 

 

Sample Input Sample Output

23
23 F 28 M 23 F 40 M 36 F 23 M 23 F 24 M 23 M
28
22 M 23 F 28 M 32 F

Caso 1:
Pares Iguais: 5
F: 3
M: 2

Caso 2:
Pares Iguais: 1
F: 0
M: 1

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <stdio.h>

int main(){
    int comp, num, i;
    int  par, fem, mas;
    char c, e;
    
    i = 0;
    while(scanf("%d",&comp) != EOF){
        if(i != 0) printf("\n");
        par = 0;
        fem = 0;
        mas = 0;
        while(scanf("%d %c%c",&num,&c,&e) == 3){
            if(num == comp){
                par++;
                if(c == 'F') fem++;
                else mas++;
            }
            if(e == '\n') break;
        }
        i++;
        printf("Caso %d:\nPares Iguais: %d\nF: %d\nM: %d\n",i,par,fem,mas);
    }
    
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
23
23 F 28 M 23 F 40 M 36 F 23 M 23 F 24 M 23 M
28
22 M 23 F 28 M 32 F

Output

x
+
cmd
Caso 1:
Pares Iguais: 5
F: 3
M: 2
Caso 2:
Pares Iguais: 1
F: 0
M: 1

#2 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) throws IOException {
		int cases = 1, F, M, i;
		String N, shoe, gender;
		String[] shoes;
		while ((N = read()) != null) {
			shoes = read().split("\\s");
			i = 0;
			F = 0;
			M = 0;
			while (i  <  shoes.length) {
				shoe = shoes[i++];
				gender = shoes[i++];
				if (N.equals(shoe)) {
					switch (gender) {
						case "F":
							F++;
							break;
						case "M":
							M++;
							break;

					}
				}
			}
			if (cases != 1) {
				out.println();
			}
			out.printf("Caso %d:\n", cases++);
			out.println("Pares Iguais: " + (F + M));
			out.println("F: " + F);
			out.println("M: " + M);
		}
		out.close();
	}

	private static String read() throws IOException {
		return in.readLine();
	}
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
23
23 F 28 M 23 F 40 M 36 F 23 M 23 F 24 M 23 M
28
22 M 23 F 28 M 32 F

#3 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("node:fs")

const input = readFileSync("/dev/stdin", "utf8")
	.split("\n")
	.map(line => line.split(" "))

function main() {
	const output = []

	for (let i = 1; input.length > 0; i += 1) {
		const [queriedShoesNumber] = input.shift()
		if (queriedShoesNumber === "") break
		const shoesList = input.shift()

		const shoes = {
			F: new Map(),
			M: new Map(),
		}

		while (shoesList.length) {
			const [num, gender] = shoesList.splice(0, 2)

			switch (gender) {
				case "F": shoes.F.set(num, (shoes.F.get(num) || 0) + 1); break
				case "M": shoes.M.set(num, (shoes.M.get(num) || 0) + 1); break
			}
		}

		const totalFemalesQueriedShoes = shoes.F.get(queriedShoesNumber) || 0
		const totalMalesQueriedShoes = shoes.M.get(queriedShoesNumber) || 0

		output.push(
			`Caso ${i}:`,
			`Pares Iguais: ${totalFemalesQueriedShoes + totalMalesQueriedShoes}`,
			`F: ${totalFemalesQueriedShoes}`,
			`M: ${totalMalesQueriedShoes}`,
			""
		)
	}

	output.pop() // Removing last blank line to prevent presentation error
	console.log(output.join("\n"))
}

main()

Copy The Code & Try With Live Editor

Input

x
+
cmd
23
23 F 28 M 23 F 40 M 36 F 23 M 23 F 24 M 23 M
28
22 M 23 F 28 M 32 F

#4 Code Example with Python Programming

Code - Python Programming


c = 1
while True:
    try:
        n = int(input())
        e = str(input()).split()
        for i in range(0, len(e), 2):
            if e[i] != str(n):
                e[i] = '*'
                e[i + 1] = '*'
        if c != 1: print()
        print('Caso {}:'.format(c))
        print('Pares Iguais:', e.count(str(n)))
        print('F:', e.count('F'))
        print('M:', e.count('M'))
        c += 1
    except EOFError:
        break

Copy The Code & Try With Live Editor

Input

x
+
cmd
23
23 F 28 M 23 F 40 M 36 F 23 M 23 F 24 M 23 M
28
22 M 23 F 28 M 32 F
Advertisements

Demonstration


Previous
#1217 Beecrowd Online Judge Solution 1217 Getline Two - Fruits Solution in C, C++, Java, Js and Python
Next
#1225 Beecrowd Online Judge Solution 1225 Perfect Choir Solution in C, C++, Java, Js and Python