Solve elementary school problems with mainstream programming languages

Recently, I saw a video on the Internet. The content is that my grandma has 60 yuan. I went to the supermarket to buy fruit for 10 yuan. How much should the cashier pay for my grandma? At first, I thought it was 50 yuan, but after thinking about it, there is no explanation in the question. How is the 60 yuan composed? It may be one piece of 50 yuan and one piece of 10 yuan, or three pieces of 20 yuan, etc. So I want to know all the possible programming languages ​​​​that we can use. Next, I will use c language, Java, Python, JavaScript and other languages ​​to write

1, c language version

#include <stdio.h>

void findChange(int total_money, int fruit_cost) {
    int change = total_money - fruit_cost;

    printf("所有可能的找零方式:\n");

    for (int x = 0; x <= change / 50; x++) {
        for (int y = 0; y <= (change - x * 50) / 20; y++) {
            for (int z = 0; z <= (change - x * 50 - y * 20) / 10; z++) {
                for (int w = 0; w <= (change - x * 50 - y * 20 - z * 10) / 5; w++) {
                    int v = change - x * 50 - y * 20 - z * 10 - w * 5;

                    printf("50元纸币:%d张,20元纸币:%d张,10元纸币:%d张,5元纸币:%d张,1元硬币:%d个\n", x, y, z, w, v);
                }
            }
        }
    }
}

int main() {
    int total_money = 60;  // 奶奶的总钱数
    int fruit_cost = 10;   // 水果的价格

    findChange(total_money, fruit_cost);

    return 0;
}

This code uses nested loops to calculate all possible change options for a given total amount and item price.

As long as the appropriate total amount and item price are passed in, the code will output all possible change schemes, including the number of 50 bills, 20 bills, 10 bills, 5 bills, and 1 coins.

2. Java Edition

public class l1 {
    public static void findChange(int total_money, int fruit_cost) {
        int change = total_money - fruit_cost;

        System.out.println("所有可能的找零方式:");

        for (int x = 0; x <= change / 50; x++) {
            for (int y = 0; y <= (change - x * 50) / 20; y++) {
                for (int z = 0; z <= (change - x * 50 - y * 20) / 10; z++) {
                    for (int w = 0; w <= (change - x * 50 - y * 20 - z * 10) / 5; w++) {
                        int v = change - x * 50 - y * 20 - z * 10 - w * 5;
                        
                        System.out.printf("50元纸币:%d张,20元纸币:%d张,10元纸币:%d张,5元纸币:%d张,1元硬币:%d个\n", x, y, z, w, v);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int total_money = 60;  // 奶奶的总钱数
        int fruit_cost = 10;   // 水果的价格

        findChange(total_money, fruit_cost);
    }
}

This Java code is similar to the C code, using nested loops to calculate the change scheme. In the findChange method, the total amount and the product price are passed in, and all possible change solutions are calculated and output in the loop. 

3. Python version

def find_change(total_money, fruit_cost):
    change = total_money - fruit_cost

    print("所有可能的找零方式:")

    for x in range(change // 50 + 1):
        for y in range((change - x * 50) // 20 + 1):
            for z in range((change - x * 50 - y * 20) // 10 + 1):
                for w in range((change - x * 50 - y * 20 - z * 10) // 5 + 1):
                    v = change - x * 50 - y * 20 - z * 10 - w * 5
                    print(f"50元纸币:{x}张,20元纸币:{y}张,10元纸币:{z}张,5元纸币:{w}张,1元硬币:{v}个")

total_money = 60   # 奶奶的总钱数
fruit_cost = 10    # 水果的价格

find_change(total_money, fruit_cost)

This Python code is similar to the C code and Java code, also using nested loops to calculate the change scheme. In the find_change function, the total amount and commodity price are passed in, and all possible change schemes are calculated and output in a loop.

4, JavaScript version

<!DOCTYPE html>
<html>
<head>
  <title>找零计算器</title>
  <script>
    function findChange(totalMoney, fruitCost) {
      const change = totalMoney - fruitCost;
      
      console.log("所有可能的找零方式:");
      
      for (let x = 0; x <= Math.floor(change / 50); x++) {
        for (let y = 0; y <= Math.floor((change - x * 50) / 20); y++) {
          for (let z = 0; z <= Math.floor((change - x * 50 - y * 20) / 10); z++) {
            for (let w = 0; w <= Math.floor((change - x * 50 - y * 20 - z * 10) / 5); w++) {
              const v = change - x * 50 - y * 20 - z * 10 - w * 5;
              console.log(`50元纸币:${x}张,20元纸币:${y}张,10元纸币:${z}张,5元纸币:${w}张,1元硬币:${v}个`);
            }
          }
        }
      }
    }
    
    function calculate() {
      const totalMoney = parseInt(document.getElementById('total-money').value);
      const fruitCost = parseInt(document.getElementById('fruit-cost').value);
      
      findChange(totalMoney, fruitCost);
    }
  </script>
</head>
<body>
  <h1>找零计算器</h1>
  
  <label for="total-money">总钱数:</label>
  <input type="number" id="total-money"><br><br>
  
  <label for="fruit-cost">水果价格:</label>
  <input type="number" id="fruit-cost"><br><br>
  
  <button onclick="calculate()">计算找零</button>
</body>
</html>

This HTML code contains a title (<h1> element), two input boxes (through the <input> element and set the id attribute respectively), and a calculation button (through the <button> element and set the onclick attribute to call JavaScript function). In the JavaScript section, the findChange function is defined to calculate all possible change schemes, and the calculate function is defined to get the value from the input box and call the change function.

Open this file in your browser, and you'll see a simple change calculator interface. When you fill in the total amount of money and fruit price, and click the calculate button, all possible change schemes will be output in the console of the browser.

The results of the four compilations are as follows:

所有可能的找零方式:
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:0张,1元硬币:50个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:1张,1元硬币:45个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:2张,1元硬币:40个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:3张,1元硬币:35个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:4张,1元硬币:30个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:5张,1元硬币:25个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:6张,1元硬币:20个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:7张,1元硬币:15个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:8张,1元硬币:10个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:9张,1元硬币:5个
50元纸币:0张,20元纸币:0张,10元纸币:0张,5元纸币:10张,1元硬币:0个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:0张,1元硬币:40个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:1张,1元硬币:35个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:2张,1元硬币:30个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:3张,1元硬币:25个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:4张,1元硬币:20个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:5张,1元硬币:15个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:6张,1元硬币:10个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:7张,1元硬币:5个
50元纸币:0张,20元纸币:0张,10元纸币:1张,5元纸币:8张,1元硬币:0个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:0张,1元硬币:30个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:1张,1元硬币:25个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:2张,1元硬币:20个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:3张,1元硬币:15个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:4张,1元硬币:10个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:5张,1元硬币:5个
50元纸币:0张,20元纸币:0张,10元纸币:2张,5元纸币:6张,1元硬币:0个
50元纸币:0张,20元纸币:0张,10元纸币:3张,5元纸币:0张,1元硬币:20个
50元纸币:0张,20元纸币:0张,10元纸币:3张,5元纸币:1张,1元硬币:15个
50元纸币:0张,20元纸币:0张,10元纸币:3张,5元纸币:2张,1元硬币:10个
50元纸币:0张,20元纸币:0张,10元纸币:3张,5元纸币:3张,1元硬币:5个
50元纸币:0张,20元纸币:0张,10元纸币:3张,5元纸币:4张,1元硬币:0个
50元纸币:0张,20元纸币:0张,10元纸币:4张,5元纸币:0张,1元硬币:10个
50元纸币:0张,20元纸币:0张,10元纸币:4张,5元纸币:1张,1元硬币:5个
50元纸币:0张,20元纸币:0张,10元纸币:4张,5元纸币:2张,1元硬币:0个
50元纸币:0张,20元纸币:0张,10元纸币:5张,5元纸币:0张,1元硬币:0个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:0张,1元硬币:30个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:1张,1元硬币:25个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:2张,1元硬币:20个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:3张,1元硬币:15个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:4张,1元硬币:10个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:5张,1元硬币:5个
50元纸币:0张,20元纸币:1张,10元纸币:0张,5元纸币:6张,1元硬币:0个
50元纸币:0张,20元纸币:1张,10元纸币:1张,5元纸币:0张,1元硬币:20个
50元纸币:0张,20元纸币:1张,10元纸币:1张,5元纸币:1张,1元硬币:15个
50元纸币:0张,20元纸币:1张,10元纸币:1张,5元纸币:2张,1元硬币:10个
50元纸币:0张,20元纸币:1张,10元纸币:1张,5元纸币:3张,1元硬币:5个
50元纸币:0张,20元纸币:1张,10元纸币:1张,5元纸币:4张,1元硬币:0个
50元纸币:0张,20元纸币:1张,10元纸币:2张,5元纸币:0张,1元硬币:10个
50元纸币:0张,20元纸币:1张,10元纸币:2张,5元纸币:1张,1元硬币:5个
50元纸币:0张,20元纸币:1张,10元纸币:2张,5元纸币:2张,1元硬币:0个
50元纸币:0张,20元纸币:1张,10元纸币:3张,5元纸币:0张,1元硬币:0个
50元纸币:0张,20元纸币:2张,10元纸币:0张,5元纸币:0张,1元硬币:10个
50元纸币:0张,20元纸币:2张,10元纸币:0张,5元纸币:1张,1元硬币:5个
50元纸币:0张,20元纸币:2张,10元纸币:0张,5元纸币:2张,1元硬币:0个
50元纸币:0张,20元纸币:2张,10元纸币:1张,5元纸币:0张,1元硬币:0个
50元纸币:1张,20元纸币:0张,10元纸币:0张,5元纸币:0张,1元硬币:0个

进程已结束,退出代码0

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/131742322