Table of contents
7-18 The mood of Big Ben
Original title:
Some netizens asked: Will there be more Big Ben questions in the future? Ben Zhong replied: It depends on your mood...
This question asks you to write a program for Big Ben to automatically output answers based on your mood.
Input format:
The input gives 24 integers in the range [0, 100] in one line, which in turn represent the mood index of Big Ben for each hour of the 24 hours a day.
Then there are several lines, each line gives an integer between [0, 23], representing the time point when netizens asked Ben Chong this question. When an illegal time point occurs, it means that the input is over, and this illegal input does not need to be processed. The question is guaranteed to be asked at least once.
Output format:
For each question, if Ben Chong's mood index is greater than 50, output it in one line 心情指数 Yes
, otherwise output it 心情指数 No
.
.
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
. - Parse the input and process the sequence of numbers: assign
buf
the first element in the array to a variabletimes
, separate it into a space-separated array of strings, and usemap
methods to convert each string to a number. Convert the substringsbuf
starting from the second element (that is, the position with index 1) to the last element (including the last element) of the array into numbers and assign them to variablesarr
. Usefilter
the method to filter out numbers that do not meet the criteria (less than 0 or greater than 23), and then store the remaining numbers inarr
. - Output the result as required: iterate through
arr
each number in the array, accesstimes
the array as an index, and output if the value corresponding to the number is greater than 50, otherwisetimes[i] + " Yes"
outputtimes[i] + " No"
. - Output result: Output the processed result string.
.
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', () => {
const times = buf[0].split(" ").map(Number);
const arr = buf.splice(1).map(Number).filter(i => 0 <= i && i <= 23);
if (arr.length < 1) {
return;
}
arr.forEach(i => console.log(`${
times[i] > 50 ? times[i] + ` Yes` : times[i] + ` No`}`));
});
.