Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1129

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

Optical Reader

 

Maratona de Programação da SBC Brazil

Timelimit: 1

Professor John decided to apply only multiple-choice tests to his students. In each test, each question will have five alternatives (A, B, C, D and E), and the teacher will distribute one answer sheet for each student. At the end of the test, the answer sheets will be scanned and processed digitally to obtain the test score for each student. Initially, he asked a nephew, who knows computer programming, to write a program to extract the alternatives marked by the students in the answer sheets. The nephew wrote a good piece of software, but he couldn't finish it because he needs to study for the ICPC contest.


During processing, the answer sheets are scanned in gray levels between 0 (full black) and 255 (total white). After detecting the position for the five rectangles corresponding to each of the alternatives of a question, the software calculates the average pixel gray level for each rectangle, returning an integer value corresponding to each alternative. If the rectangle was filled properly the average value is zero (all black). If the rectangle was left blank the average value is 255 (white total). Thus, ideally, if the average values for each alternative of a question are (255, 0, 255, 255, 255), we know that the student has marked alternative B for that question. However, as answer sheets are processed individually, the average gray level for a completely filled rectangle is not necessarily 0 (may be higher), and the value for a rectangle not filled is not necessarily 255 (may be less). Professor John determined that rectangle average gray levels would be divided into two classes: those with values equal or lower to 127 are considered black and those with values higher than 127 will be considered white.

Obviously, not necessarily all questions of all sheets are marked correctly. It can happen that a student makes a mistake and marks more than one alternative for the same question, or does not mark any alternative. In such cases, the answer to the question should be disregarded.

Now, professor John needs a volunteer to write one program that, given the average gray level values of the five rectangles corresponding to the alternatives of a question, determines which alternative was marked, or whether the answer to the question should be disregarded.

 

Input

 

The input contains several test cases. The first line of a test case contains an integer N indicating the number of questions in the answer sheet (1 ≤ N ≤ 255).Each of the N following lines describes the response to a question and contains five integers A, B, C, D and E, indicating the values of average gray levels for each alternative (0 ≤ A, B, C, D, E ≤ 255).

The last test case is followed by a line containing only a number zero.

 

Output

 

For each input test case, your program must print N lines, each line corresponding to a question. If the answer to the question was correctly filled in the answer sheet, the line should contain the alternative selected (‘A’,‘B’, ‘C’, ‘D’ or ‘E’). Otherwise, the line should contain the character ‘*’ (asterisk).

 

 

 

Input Sample Output Sample

3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

A
E
C
D
*
*
B

Code Examples

#1 Code Example with C Programming

Code - C Programming


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

char alternativaPrint(unsigned short);

int main (void)
{

	unsigned char alternativas[6];
	unsigned short numQuestoes, i;
	unsigned short contador, posicao;
	bool unica;

	while (true)
	{

		scanf("%hu", &numQuestoes);

		if (numQuestoes == 0)
			break;

		while (numQuestoes--)
		{

			for (i = 0; i  <  5; i++)
				scanf("%hhd", &alternativas[i]);

			unica = true;
			contador = 0;
			for (i = 0; i  <  5; i++)
				if (alternativas[i] <= 127)
				{
					contador++;
					if (contador == 1 && unica == true)
					{
						unica = false;
						posicao = i;
					}

				}

			if (!contador)
				printf("*\n");
			else if (contador == 1)
				printf("%c\n", alternativaPrint(posicao));
			else
				printf("*\n");

		}
	}
}

char alternativaPrint(unsigned short posicao)
{

	switch (posicao) {
		case 0:
			return 'A';
		case 1:
			return 'B';
		case 2:
			return 'C';
		case 3:
			return 'D';
		case 4:
			return 'E';
	}

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

Output

x
+
cmd
A
E
C
D
*
*
B

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int n, x;
    string s="ABCDE";
    while (cin >> n)
    {
        if (n==0)
            break;
        while (n--)
        {
            vector<int>v;
            for (int i = 0; i  <  5; ++i)
            {
                cin  >>  x;
                if (x  < = 127)
                    v.push_back(i);
            }
            if (v.size() != 1)
                cout << "*" << endl;
            else
                cout << s[v[0]] << endl;
        }
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

Output

x
+
cmd
A
E
C
D
*
*
B

#3 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);
    static final char[] ANSWERS = {'A', 'B', 'C', 'D', 'E'};

    public static void main(String[] args) throws IOException {
        String l;
        int N;
        char answer;
        String[] marks;
        int graylevel;
        while (!(l = read()).equals("0")) {
            N = toInt(l);
            while (N-- > 0) {
                answer = '*';
                marks = read().split("\\s");
                for (int i = 0; i  <  5; i++) {
                    graylevel = toInt(marks[i]);
                    if (graylevel  < = 127) {
                        if (answer == '*') {
                            answer = ANSWERS[i];
                        } else {
                            answer = '*';
                            break;
                        }
                    }
                }
                out.println(answer);
            }
        }
        out.close();
    }

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

    private static int toInt(String s) {
        return Integer.parseInt(s);
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

Output

x
+
cmd
A
E
C
D
*
*
B

#4 Code Example with Python Programming

Code - Python Programming


while True:
    n = int(input())
    if n == 0: break
    for i in range(n):
        e = [int(x) for x in str(input()).split()]
        c = []
        for j in e:
            if j <= 127: c.append(0)
            else: c.append(255)
        if c.count(0) == 1:
            if c[0] == 0: print('A')
            elif c[1] == 0: print('B')
            elif c[2] == 0: print('C')
            elif c[3] == 0: print('D')
            elif c[4] == 0: print('E')
        else: print('*')
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

Output

x
+
cmd
A
E
C
D
*
*
B

#5 Code Example with Javascript Programming

Code - Javascript Programming


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

function filledOrEmpty(shade) {
	const inRange = ((value) => (min, max = value) => value === Math.min(Math.max(min, value), max))(+shade)

	if (inRange(0, 127)) return "B"
	else if (inRange(128, 255)) return "W"
	else return ""
}

const output = []
const AlternativesEnum = { 0: "A", 1: "B", 2: "C", 3: "D", 4: "E" }

while (input.length > 0) {
	const numQuestions = Number.parseInt(input.shift(), 10)

	if (numQuestions == 0) break

	const questions = input.splice(0, numQuestions).map(quest => quest.split(" ").map(filledOrEmpty))

	for (const quest of questions) {
		if (quest.filter(alt => alt == "B").length != 1) output.push("*")
		else output.push(AlternativesEnum[quest.indexOf("B")])
	}
}

console.log(output.join("\n"))
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
0 255 255 255 255
255 255 255 255 0
255 255 127 255 255
4
200 200 200 0 200
200 1 200 200 1
1 2 3 4 5
255 5 200 130 205
0

Output

x
+
cmd
A
E
C
D
*
*
B
Advertisements

Demonstration


Previous
#1127 Beecrowd Online Judge Solution 1127 Musical Plagiarism Solution in C, C++, Java, Js and Python
Next
#1130 Beecrowd Online Judge Solution 1130 Tic-tac-toe Solution in C, C++, Java, Js and Python