Table of contents
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 1
representing black and 2
yellow.
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 1
to represent black and 2
yellow, 0
it 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:
- Import
readline
the module and create an interface object: Firstreadline
introduce the module and usecreateInterface
methods to create an interface objectrl
. This object sets the input stream to standard input. - Read input and store: By listening to
'line'
events, the input is stored in an arraybuf
. - Deconstruct the assignment to get the correct answer and student answer: use
split
the method to split the second element (that is, the correct answer array) by spaces, and usemap
the method to convert each element into a number and store it in a variable; use the method to convertans
the fourth element and subsequent The elements (i.e. the array of student answers) are stored in a variabletext
, which in this casetext
is an array of strings. - Iterate over the student answers array: Use
for...of
a looptext
to iterate over each elementtemp
. - Determine whether the student answer array is exactly the same as the correct answer array: use two variables
flag
andk
, whereflag
the initial value istrue
,k
indicating the number of 0 elements in the student answer array.temp
For each element traversed using the inner looptemp[j]
, iftemp[j]
it is 0, it will bek
incremented by 1; otherwise, if ittemp[j]
isans[j]
equal to, the loop will continue; otherwise, it will beflag
set tofalse
. - Output according to the judgment result: if
k
equaltemp.length
, it means that the student answer array is all 0, then output "Ai Ya"; otherwise, if itflag
istrue
, it means that the student answer array is exactly the same as the correct answer array, then output "Da Jiang!!!"; otherwise Output "Ai Ya". - Output the result after the loop ends: use
console.log
the 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");
}
}
});
.