编辑代码

#include <stdio.h>

void makeChange(int coins[], int numCoins, int amount) {
    // 对货币面额进行降序排序
    for (int i = 0; i < numCoins - 1; i++) {
        for (int j = 0; j < numCoins - i - 1; j++) {
            if (coins[j] < coins[j + 1]) {
                int temp = coins[j];
                coins[j] = coins[j + 1];
                coins[j + 1] = temp;
            }
        }
    }

    // 初始化结果数组
    int result[numCoins];
    for (int i = 0; i < numCoins; i++) {
        result[i] = 0;
    }

    // 贪心选择面额最大的货币
    for (int i = 0; i < numCoins; i++) {
        while (coins[i] <= amount) {
            result[i]++;
            amount -= coins[i];
        }
    }

    // 输出结果
    printf("找零方案:\n");
    for (int i = 0; i < numCoins; i++) {
        if (result[i] > 0) {
            printf("%d元 x %d\n", coins[i], result[i]);
        }
    }
}

int main() {
    int coins[] = {25, 10, 5, 1};  // 货币面额
    int numCoins = sizeof(coins) / sizeof(coins[0]);  // 货币面额数量
    int amount = 42;  // 需要支付的金额

    makeChange(coins, numCoins, amount);

    return 0;
}