C++ Practice Problem – The Hundred Money Hundred Chickens Problem

Time Limit: 2s Memory Limit: 192MB

Problem Description

The famous “Hundred Money Hundred Chickens Problem” was proposed by the ancient Chinese mathematician Zhang Qiujian in his book “Mathematical Classics”: A rooster costs 5 coins, a hen costs 3 coins, and 3 chicks cost 1 coin. With 100 coins, how many roosters, hens, and chicks can be bought?

Input Format

No input

Output Format

Provide all solutions, with each solution on a new line

The order of solutions: sorted in “lexicographical order”, meaning fewer roosters come first; if the number of roosters is the same, fewer hens come first

Format:

cock=%d,hen=%d,chicken=%d

Sample Input

No input

Sample Output

cock=0,hen=25,chicken=75

cock=4,hen=18,chicken=78

cock=8,hen=11,chicken=81

cock=12,hen=4,chicken=84

Code

#include <iostream>
using namespace std;
// Function: Solve the Hundred Money Hundred Chickens Problem
void solveChickenProblem() {
    for (int cock = 0; cock <= 20; cock++) {
        for (int hen = 0; hen <= 33; hen++) {
            int chicken = 100 - cock - hen;
            if (chicken % 3 == 0) {  // The number of chicks must be a multiple of 3
                int total_money = 5 * cock + 3 * hen + chicken / 3;
                if (total_money == 100) {
                    cout << "cock=" << cock << ",hen=" << hen << ",chicken=" << chicken << endl;
                }
            }
        }
    }
}
int main() {
    solveChickenProblem();
    return 0;
}

Output Result

C++ Practice Problem - The Hundred Money Hundred Chickens Problem

Code Explanation

  1. Outer Loop: Iterate over the number of roosters (0-20)

  2. Inner Loop: Iterate over the number of hens (0-33)

  3. Calculate the Number of Chicks: chicken = 100 – cock – hen

  4. Check Conditions:

  • The number of chicks must be a multiple of 3 (since 3 chicks cost 1 coin)

  • The total money must equal 100 coins

  • Output Result: Output the combinations that meet the conditions in the required format

  • Problem Analysis

    This is a classic mathematical problem that requires finding all integer solutions that satisfy the following conditions:

    • Rooster (cock) costs 5 coins each

    • Hen (hen) costs 3 coins each

    • Chick (chicken) costs 1 coin for 3 chicks

    • Total money is 100 coins, total chickens is 100

    Methodology

    1. Establish Equations:

    • cock + hen + chicken = 100

    • 5*cock + 3*hen + chicken/3 = 100

  • Iterate All Possibilities: Since the maximum number of roosters is 20 and hens is 33, all possible combinations can be iterated.

  • Check Conditions: For each combination, check if it meets the total money and total chicken conditions.

  • C++ Practice Problem - The Hundred Money Hundred Chickens Problem

    C++ Basic Tutorial Collection

    C++ Practice Problem - The Hundred Money Hundred Chickens ProblemC++ Basic Materials

    1. C++ Output

    2. C++ Variables

    3. C++ Input

    4. C++ Expressions

    5. IF Statements

    6. IF Applications

    7. WHILE Loop Statements

    8. FOR Loop Statements

    9. Arrays

    10. One-Dimensional Arrays

    11. Two-Dimensional Arrays

    12. C++ Functions

    13. C++ File Operations – Writing Files

    If you find it interesting, just click on me

    C++ Practice Problem - The Hundred Money Hundred Chickens ProblemC++ Practice Problem - The Hundred Money Hundred Chickens ProblemC++ Practice Problem - The Hundred Money Hundred Chickens ProblemC++ Practice Problem - The Hundred Money Hundred Chickens Problem

    Leave a Comment