Pintia problem solution - 7-18 The mood of Big Ben

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:

  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. Parse the input and process the sequence of numbers: assign bufthe first element in the array to a variable times, separate it into a space-separated array of strings, and use mapmethods to convert each string to a number. Convert the substrings bufstarting 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 variables arr. Use filterthe method to filter out numbers that do not meet the criteria (less than 0 or greater than 23), and then store the remaining numbers in arr.
  4. Output the result as required: iterate through arreach number in the array, access timesthe array as an index, and output if the value corresponding to the number is greater than 50, otherwise times[i] + " Yes"output times[i] + " No".
  5. 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`}`));
});

.

Complexity analysis:

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

Guess you like

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