Pintia problem solution - 7-13 Find the three-digit narcissus number

7-13 Find the three-digit narcissus number

Original title:

This question requires writing a program to output all three-digit narcissus numbers in the interval between given positive integers M and N. A three-digit daffodil number, that is, the sum of the cubes of its ones, tens, and hundreds digits is equal to the number itself.

Input format:

The input is given as two positive integers M and N in one line (100≤ MN ≤999).

Output format:

Output all three-digit narcissus numbers in the M and N intervals sequentially, one number in each line. If there is no three-digit narcissus number in this interval, there is no output.

If M or N does not meet the requirements of the question, output it Invalid Value..

.

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, store each line of input in an array buf.
  3. Parse the input and validate it: by buf[0].split(" ").map(Number)converting the input into an array containing two values arr. Assign the first value to the variable mand the second value to the variable respectively n. At the same time, the validity of the sum is mchecked n. If the conditions are not met, "Invalid Value." is output and returned.
  4. Find the number of daffodils: Create an empty array res. Use a loop to iterate through each number forfrom mto . nFor each number, call a function isDaffodilto determine whether it is a narcissus number. If so, add it to the array res.
  5. Output result: If resthe length of the array is less than 1, it means that the number of daffodils is not found in the given range and is returned directly. Otherwise, use for...ofa loop to iterate through the array resand output the number of each daffodil.
  6. Define function isDaffodil: This function is used to determine whether a number is a narcissus number. Convert the number to a string and iterate over each numeric character, calculating the sum of its cubes. Returns if the sum of cubes is equal to the original number, trueotherwise false.

.

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 arr = buf[0].split(" ").map(Number)
    let m = arr[0]
    let n = arr[1]
    if (!(100 <= m && m <= n && n <= 999)) {
    
    
        console.log("Invalid Value.")
        return
    }


    let res = []
    for (let i = m; i <= n; i++) {
    
    
        if (isDaffodil(i)) {
    
    
            res.push(i)
        }
    }

    if (res.length < 1) {
    
    
        return
    } else {
    
    
        for (let i of res) {
    
    
            console.log(i);
        }
    }

});

function isDaffodil(num) {
    
    
    let str = "" + num
    let sum = 0
    for (let i of str) {
    
    
        sum += parseInt(i) ** 3
    }
    return num == sum
}

.

Complexity analysis:

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

Guess you like

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