Pintia Problem Solution——7-27 Hat Guessing Game

7-27 Hat guessing game

Original title:

The babies play a guessing hat game together. A hat was placed on each person's head, some were black and some were yellow. Everyone can see the hats on other people's heads, but not their own. After the game starts, everyone can guess the color of the hat on their head, or they can abstain from guessing. If no one guesses wrong and at least one person guesses correctly, then all the babies will win a grand prize. If no one guesses, or if only one person guesses wrong, no prize will be given to all the babies.
The following order gives the colors of a row of hats. It is assumed that when each group of babies comes to play, hats are distributed in this order. Then the guess results of each group of babies are given, and you are asked to judge whether they can win the grand prize.

Input format:

The input first gives a positive integer N (2< N ≤100) in a line , which is the number of hats. The second line gives the colors of N hats, with numbers 1representing black and 2yellow.
Next, a positive integer K (≤10) is given below, followed by K lines. Each line gives the guessing results of a group of babies. In addition to still using numbers 1to represent black and 2yellow, 0it means that the baby abstains from guessing.
Numbers on the same line are separated by spaces.

Output format:

For each group of babies playing the game, if they can win the grand prize, output it in one line Da Jiang!!!, otherwise output it Ai Ya.

.

Problem-solving ideas:

  1. Import readlinethe module and create an interface object: First readlineintroduce the module and use createInterfacemethods to create an interface object rl. This object sets the input stream to standard input.
  2. Read input and store: By listening to 'line'events, the input is stored in an array buf.
  3. Deconstruct the assignment to get the correct answer and student answer: use splitthe method to split the second element (that is, the correct answer array) by spaces, and use mapthe method to convert each element into a number and store it in a variable; use the method to convert ansthe fourth element and subsequent The elements (i.e. the array of student answers) are stored in a variable text, which in this case textis an array of strings.
  4. Iterate over the student answers array: Use for...ofa loop textto iterate over each element temp.
  5. Determine whether the student answer array is exactly the same as the correct answer array: use two variables flagand k, where flagthe initial value is true, kindicating the number of 0 elements in the student answer array. tempFor each element traversed using the inner loop temp[j], if temp[j]it is 0, it will be kincremented by 1; otherwise, if it temp[j]is ans[j]equal to, the loop will continue; otherwise, it will be flagset to false.
  6. Output according to the judgment result: if kequal temp.length, it means that the student answer array is all 0, then output "Ai Ya"; otherwise, if it flagis true, it means that the student answer array is exactly the same as the correct answer array, then output "Da Jiang!!!"; otherwise Output "Ai Ya".
  7. Output the result after the loop ends: use console.logthe judgment result to output each student's answer array.

.

JavaScript (node) code:

const r = require("readline");
const rl = r.createInterface({
    
    
    input: process.stdin
});
let buf = [];
rl.on('line', (input) => buf.push(input));
rl.on('close', () => {
    
    
    let ans = buf[1].split(" ").map(Number);
    let text = buf.slice(3);

    for (let i = 0; i < text.length; i++) {
    
    
        let temp = text[i].split(" ").map(Number);
        let flag = true;
        let k = 0;
        for (let j = 0; j < temp.length; j++) {
    
    

            if (temp[j] == 0) {
    
    
                k++;
            } else if (temp[j] == ans[j]) {
    
    
                continue;
            } else {
    
    
                flag = false
            }

        }
        if (k == temp.length) {
    
    
            console.log("Ai Ya");
        } else {
    
    
            console.log(flag ? "Da Jiang!!!" : "Ai Ya");
        }
    }

});

.

Complexity analysis:

Time complexity: O(n*m)
Space complexity: O(n)

Guess you like

Origin blog.csdn.net/Mredust/article/details/133519976