Pintia problem solution - 7-25 programmers buy buns

7-25 Programmers buy buns

.

Original title:

This is a joke to test real programmers: If you are asked by your family to buy ten steamed buns on the way after get off work, if you see a watermelon seller, buy one. So under what circumstances would you buy just one bun and take it home?
I ask you to consider the general version of this joke: If you are asked to buy N steamed buns on the way home from work, if you see someone selling X , buy M steamed buns. So if you finally buy K steamed buns and go home, does that mean you saw someone selling X ?

Input format:

Enter the N , X , M , K in the question in order on one line , separated by spaces. Among them, N , M and K are positive integers not exceeding 1000, and X is a string with a length not exceeding 10 and consisting only of lowercase English letters. The question guarantees N \= M.

Output format:

Output the conclusion on one line in the format:

  • If K = N , output mei you mai X de;
  • If K = M , output kan dao le mai X de;
  • Otherwise output wang le zhao mai X de.
    where is the string XX given in the input .

.

Problem-solving ideas:

  1. Known data: N , X , M , K
  2. Problem-solving ideas: Write code according to the output format
    • If K = N , output mei you mai X de;
    • If K = M , output kan dao le mai X de;
    • Otherwise output wang le zhao mai X de.
    • where is the string XX given in the input .

.

JavaScript (node) code:

const r = require("readline");
const rl =r.createInterface({
    
    
    input:process.stdin,
    output: process.stdout
});

rl.question('',(input)=>{
    
    
    const [n,x,m,k] = input.split(" ");
    handlePurchase(parseInt(n),x,parseInt(m),parseInt(k));
    rl.close();
});

//根据输出格式编写函数
function handlePurchase(n, x, m, k) {
    
    
    if(k == n){
    
    
        console.log(`mei you mai ${
      
      x} de`);
    }else if (k == m){
    
    
        console.log(`kan dao le mai ${
      
      x} de`);
    }else{
    
    
        console.log(`wang le zhao mai ${
      
      x} de`);
    }
}

.

Complexity analysis:

  • Time complexity: O(1)
  • Space complexity: O(1)

Guess you like

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