Pintia problem solution——7-28 Who cares who calls daddy?

7-28 Who cares who calls me daddy?

Original title:

"Who cares who calls me daddy" is a funny rap song on the Internet, originating from the entertainment game played on the Northeastern wine table. Now let's increase the difficulty of this game and use more IQ.
Let's assume that the two people in the game are A and B. After the game starts, the two people report two integers N**A​ and N**B​ at the same time . The criteria for determining who is the father are as follows:

  • Add the digits of two integers to get two sums S**A and S**B . If N**A is exactly an integer multiple of S**B , then A is the parent; if N**B is exactly an integer multiple of S**A , then B is the parent;
  • If two people meet or do not meet the above judgment conditions at the same time, the one with the higher original number is the father.
    This question asks you to write an automatic referee program to determine who is the father.

Input format:

The first line of input gives a positive integer N (≤100), which is the number of games. The following N lines, each line gives a pair of positive integers not exceeding 9 digits, corresponding to the original numbers given by A and B. The question ensures that the two numbers are not equal.

Output format:

AFor each round of play, give the player( s ) who won the title of "Dad" in a row B.

.

Problem-solving ideas:

  1. Import readlinethe module and create an interface object: Same as the previous question, readlineintroduce the module and use createInterfacethe method to create an interface object rl. This object sets the input stream to standard input.
  2. Read input and store: Same as the previous question, by listening to 'line'events, store the input in an array buf.
  3. splitDeconstruct the assignment to obtain Na, Nb, Sa and Sb: use the method to split the i-th element (i.e. the i-th group of input) by spaces, and use mapthe method to convert each element into a number and store it in a variable nums; store the nums[0]and nums[1]respectively in In the variable Nasum , the sum Nbof each digit is also stored in the variable.SaSb
  4. Determine the size of Na and Nb: first determine whether Na is equal to Nb, and return directly if equal; otherwise, continue to perform subsequent steps.
  5. Output results based on conditions: Use if...else if...elsestatement keywords to judge the following conditions in sequence and output the results:
    • If Na can divide Sb and Nb can divide Sa, the larger number letter "A" or "B" will be output, which is determined by the relationship between Na and Nb.
    • If Na can divide Sb but Nb cannot divide Sa, then output "A".
    • If Na cannot divide Sb but Nb can divide Sa, then output "B".
    • If neither Na nor Nb satisfies conditions 1, 2 and 3, the output "A" or "B" will be determined based on the relationship between Na and Nb.

.

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 n = parseInt(buf[0])
    if (n > 100) {
    
    
        return
    }
    for (let i = 1; i <= n; i++) {
    
    
        let nums = buf[i].split(" ")
        let Na = parseInt(nums[0])
        let Nb = parseInt(nums[1])
        if (Na == Nb) {
    
    
            return
        }

        let Sa = 0, Sb = 0;
        for (let i of nums[0]) {
    
    
            Sa += parseInt(i);
        }
        for (let i of nums[1]) {
    
    
            Sb += parseInt(i);
        }

        if ((Na % Sb === 0) && (Nb % Sa === 0)) {
    
    
            console.log(Na > Nb ? "A" : "B");
        } else if (Na % Sb === 0) {
    
    
            console.log("A");
        } else if (Nb % Sa === 0) {
    
    
            console.log("B");
        } else {
    
    
            console.log(Na > Nb ? "A" : "B");
        }


    }

});

.

Complexity analysis:

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

Guess you like

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