Table of contents
7-19 Kindness in Ladder Match
Original title:
Ladder match is a kind game. The kind-hearted question group hopes to control the difficulty of the questions within a range so that every participating student can have questions that can be solved, and the most powerful students have to work very hard to get high scores.
So the proposition team first divided programming ability into 106 levels (it’s crazy, it’s fake), and then investigated the programming ability of each participating student. Now please write a program to find out the minimum and maximum ability values of all participating students, and give it to the proposition group as a reference for setting questions.
Input format:
The input gives a positive integer N (≤2×104) in the first line , which is the total number of participating students. The next line gives N positive integers not exceeding 106, which are the ability values of participating students.
Output format:
The first line outputs the minimum ability value of all participating students and the number of students with this ability value. The second line outputs the maximum ability value of all participating students and the number of students with this ability value. Numbers in the same row are separated by 1 space, and there must be no extra spaces at the beginning or end of the line. .
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 second element in the array (that is, the position with index 1) to a variablearr
, separate it into an array of strings based on spaces, and then usemap
the method to convert each string into a number. - Find the minimum and maximum values: Use two variables
min
andmax
, initialized toarr
the first element of . Traverse the arrayarr
, if the current element is greater than the current elementmax
, it will bemax
updated to the current element; if the current element is less than the current elementmin
, it willmin
be updated to the current element. - Count the occurrences of the minimum and maximum values: use two variables
minCount
andmaxCount
initialize to 0. Traverse the array againarr
, and if the current element is equalmin
,minCount
add 1; if the current element is equalmax
,maxCount
add 1. - Output results: Use
console.log
to output the minimum value and the number of occurrences of the minimum value, as well as the maximum value and the number of occurrences of the maximum value respectively.
.
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 arr = buf[1].split(" ").map(Number);
let min = arr[0];
let max = arr[0];
for (let i of arr) {
if (i > max) {
max = i;
}
if (i < min) {
min = i;
}
}
let minCount = 0;
let maxCount = 0;
for (let i of arr) {
if (min == i) {
minCount++;
}
if (max == i) {
maxCount++;
}
}
console.log(min + " " + minCount);
console.log(max + " " + maxCount);
});
.