简单问题,大学生初学者

古时候有一个人去市场上买鸡,公鸡5文钱1只,母鸡3文钱1只,小鸡
1文钱3只。此人用100文钱买了100只鸡,问公鸡、母鸡、小鸡各有多
少只?

#include <stdio.h>

int main() {
    int x, y, z;
    for (x = 0; x <= 20; x++) {
        for (y = 0; y <= 33; y++) {
            z = 100 - x - y;
            if (5 * x + 3 * y + z / 3 == 100 && z % 3 == 0) {
                printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
            }
        }
    }
    return 0;
}
#include <stdio.h>

int main() {
    int x, y, z; // 公鸡数量、母鸡数量、小鸡数量
    int totalMoney = 100;  // 总钱数
    int totalChicken = 100; // 总鸡数
    
    // 公鸡每只5文钱,母鸡每只3文钱,小鸡每31文钱
    int cockPrice = 5;
    int henPrice = 3; 
    int chickPrice = 1; 
    
    // 求值方程1: x + y + z = totalChicken
    // 求值方程2: 5x + 3y + z/3 = totalMoney
    
    // 从方程1得到:z = totalChicken - x - y
    // 代入方程2:
    // 5x + 3y + (totalChicken - x - y)/3 = totalMoney
    // 15x + 9y = 300
    // x + 3y = 20
    
    // 化简为:x + 3y = 20
    // 再代入方程1:
    // x + y + (totalChicken - x - y) = totalChicken
    // 2y = 80
    // y = 40
    
    // 代入方程x + 3y = 20
    // x + 120 = 20
    // x = -100   // 因为x代表公鸡数量,不能为负,所以取x = 0
    
    x = 0;
    y = 40;
    z = totalChicken - x - y;
    
    printf("公鸡:%d只\n", x);
    printf("母鸡:%d只\n", y);
    printf("小鸡:%d只\n", z);
}