编辑代码

#include <iostream>
using namespace std;
#include <vector>

// 解决百钱百鸡问题的函数
vector<vector<int>> solveChickenProblem() {
    vector<vector<int>> solutions; // 存储所有解的容器

    for (int x = 0; x <= 20; x++) { // 鸡翁的数量范围 [0, 20]
        for (int y = 0; y <= 33; y++) { // 鸡母的数量范围 [0, 33]
            int z = 100 - x - y; // 鸡雏的数量由总数量减去鸡翁和鸡母的数量得到
            if (z >= 0 && 5 * x + 3 * y + z / 3 == 100 && z % 3 == 0) {
                // 满足条件:总钱数为100,总数量为100,且鸡雏数量为3的倍数
                solutions.push_back({x, y, z}); // 将解存入容器
            }
        }
    }

    return solutions; // 返回所有解的容器
}

int main() {
    // 使用 solveChickenProblem 函数解决百钱百鸡问题
    vector<vector<int>> solutions = solveChickenProblem();

    // 打印所有解
    cout << "百钱百鸡问题的解:" << endl;
    for (const vector<int>& solution : solutions) {
        cout << "鸡翁:" << solution[0] << "只,鸡母:" << solution[1] << "只,鸡雏:" << solution[2] << "只" << endl;
    }

    return 0;
}