1 条题解

  • 0
    @ 2026-7-18 16:13:49

    1、2、5元硬币组合 题解

    解题思路

    枚举 1 元硬币数量 x 和 2 元硬币数量 y,5 元硬币数量可由总枚数直接计算:z=m-x-y。再判断总金额是否等于 n

    参考代码

    #include<bits/stdc++.h>
    using namespace std;
    
    int main() {
        int m, n;
        cin >> m >> n;
    
        int cnt = 0;
    
        for (int x = 0; x <= m; x++) {
            for (int y = 0; y <= m - x; y++) {
                int z = m - x - y;
    
                if (x + y * 2 + z * 5 == n) {
                    cout << x << " " << y << " " << z << endl;
                    cnt++;
                }
            }
        }
    
        if (cnt == 0) {
            cout << "None" << endl;
        }
    
        cout << "Count: " << cnt << endl;
        return 0;
    }
    
    • 1

    信息

    ID
    4886
    时间
    1000ms
    内存
    64MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者