首页 > 试题广场 >

百钱买百鸡问题

[编程题]百钱买百鸡问题
  • 热度指数:123737 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}公元五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
\hspace{15pt}意思是,公鸡一只 5 元,母鸡一只 3 元,小鸡三只 1 元。现在有 100 元,问公鸡、母鸡、小鸡各多少只?你只需要直接输出答案即可。

输入描述:
本题不需要读取输入。


输出描述:
输出若干行,每行包含三个整数,分别代表公鸡、母鸡、小鸡的数量。
示例1

输入

1

输出

0 25 75
4 18 78
8 11 81
12 4 84
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        for(int i=0;i<=20;i++){
            for(int j=0;j<34;j++){
                int k=100-5*i-3*j;
                if(k<0){
                    break;
                }else{
                    if(i+j+k*3==100){
                        System.out.println(i+" "+j+" "+k*3);
                    }
                }
            }
        }
    }
}

发表于 2025-04-14 22:05:09 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for (int i = 0; i <= 20; i++) {
            for (int j = 0; j <= 33; j++) {
                int k = 100 - i - j;
                if ((k % 3 == 0) && (5 * i + 3 * j + k / 3.0 == 100) ) {
                    System.out.println(i + " " + j + " " + k);
                }
            }
        }
    }
}
发表于 2025-03-27 15:46:17 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int a = in.nextInt();
            int money = 100;
            // x + y + z = 100
            // 5x + 3y + z / 3 = 100
            for (int x = 0; x <= 20; x++) {
                for (int y = 0; y <= 100 - x; y++) {
                    int z = (100 - x - y);
                    if (z % 3 > 0) {
                        continue;
                    }
                    if (5 * x + 3 * y + z / 3 == 100) {
                        System.out.println(x + " " + y + " " + z);
                    }
                }
            }
        }
    }
}
发表于 2024-10-02 16:01:57 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//毫无意义,随便接收输入已开始程序
        for (int i = 0; i <= 20; i++) { //公鸡最大数量不可能超过20
            for (int j = 0; j <= (100 - i * 5) / 3;
                    j++) { //公鸡数量每增加一只,可用金钱数量就减少5,可购买的母鸡数量就要再除以3
                int k = 100 - i -
                        j; //可购买鸡仔数量等于总数量100只减去公鸡数量和母鸡数量
                if (i + j + k == 100 &&
                        (i * 5 + j * 3 + (float)k / 3) ==
                        100.00) {  //使用整型会丢弃小数点部分,导致结果出错,所以需要将鸡崽数量除以三的结果保存为浮点型,并与100.00进行比较
                    System.out.print(i + " " + j + " " + k);
                    System.out.println();
                }
            }
        }
    }
}
发表于 2024-09-21 16:11:29 回复(0)

import java.util.Scanner;

public class Main {
    public static void main(String[] args)  {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num = in.nextInt();
            int n = 100;
            int male = n / 5; // 最多20
            int female = n / 3; // 最多33
            int child = 0;
            for (int i = 0 ; i <= male; i++) {
                for (int j = 0; j <= female; j++) {
                    child = 100 - i - j;
                    if (child % 3 == 0 && (i * 5 + j * 3 + child / 3 == n)) {
                        System.out.println(i + " " + j + " "+ child);
                    }
                }
            }
        }
    }
}


编辑于 2023-12-13 10:53:59 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.nextInt();
        for(int i = 0; i <= 100; i++){
            for(int j = 0; j <= 100 - i; j++){
                if(i*5 + 3*j + (100 - i - j) / 3.0 == 100){
                    System.out.println(i+" "+j+" "+(100-i-j));
                }
            }
        }
    }
}

发表于 2023-11-27 16:44:33 回复(0)
import java.util.Scanner;

public class HJ72 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int[][][] dp = new int[21][34][301];
            process(100, 0, 0, 0, dp);
        }
    }

    // c1 1 cost 5 money
    // c2 1 cost 3 money
    // c3 3 cost 1 money
    private static void process(int money, int c1, int c2, int c3, int[][][] dp) {
        if (money < 0 || c1 + c2 + c3 > 100) return;
        // 计算过的不用再算了,不要打印了
        if (dp[c1][c2][c3] != 0) return;
        if (c1 + c2 + c3 == 100 && money == 0) System.out.println(c1 + " " + c2 + " " + c3);
        // 标记这种c1,c2,c3的买法已经计算过了
        dp[c1][c2][c3] = 1;
        // 这种顺序跟答案对齐
        process(money - 3, c1, c2 + 1, c3, dp);
        process(money - 5, c1 + 1, c2, c3, dp);
        process(money - 1, c1, c2, c3 + 3, dp);
    }

}


发表于 2023-03-30 21:56:42 回复(1)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 34; j++) {
                    int k=100-i-j;
                    if(k%3==0){
                        if(5*i+3*j+k/3==100){
                            System.out.println(i+" "+j+" "+k);
                        }else continue;
                    }else continue;

                }
            }
        }
    }
}

发表于 2023-02-27 14:06:51 回复(0)
//各位道友,为何有这种题,哈哈哈哈
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        System.out.println("0"+" "+"25"+" "+"75");
        System.out.println("4"+" "+"18"+" "+"78");
        System.out.println("8"+" "+"11"+" "+"81");
        System.out.println("12"+" "+"4"+" "+"84");
    }
}

发表于 2023-02-11 08:36:14 回复(5)
穷举
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // 注意 hasNext 和 hasNextLine 的区别
        for(int i = 0;i<=100/5;i++){
            for(int j =0;j<=100/3;j++){
                //k一定是3的倍数
                for(int k=0;k<=300;k=k+3){
                    if((5*i+3*j+(k/3))==100 && (i+j+k)==100){
                        System.out.println(i+" "+j+" "+k);
                    }
                }
            }
        }



    }
}



发表于 2023-01-08 15:16:06 回复(0)
0 25 75
4 18 78
8 11 81
12 4 84
“花一百元买一百只鸡的方式“
11 + 81 = 92 
???题目有歧义,应该说清楚母鸡和雏鸡的数量关系并不是相加等于100这种情况。
而是金额相加等于100就行。



发表于 2022-09-18 00:13:57 回复(1)
import java.util.Scanner;

import static java.lang.System.in;

public class Main {
    public static void main(String[] args) {
        System.out.println("0 25 75\n4 18 78\n8 11 81\n12 4 84");
    }
}

发表于 2022-08-30 19:10:14 回复(1)
import java.util.Scanner;

import static java.lang.System.in;


/**
根据关系推出:14x+8y=200时,百元可以买百鸡
*/
public class Main {
    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int x = 14;
        int y = 25;
        for(int i = 0; i <= x; ++i){
            if((200 - 14 * i) % 8 == 0 && (200 - 14 * i)/8 <= y){
                System.out.print(i + " ");
                System.out.print((200 - 14 * i)/8 + " ");
                System.out.println(100 - i - (200 - 14 * i)/8);
            }
        }
    }
}

发表于 2022-07-20 17:31:50 回复(0)
给大伙整个活
public class Main {
    public static void main(String[] args) {
        System.out.println("0 " + "25 " + "75");
        System.out.println("4 " + "18 " + "78");
        System.out.println("8 " + "11 " + "81");
        System.out.println("12 " + "4 " + "84");
    }
}


发表于 2022-07-12 11:42:05 回复(2)
        for(int i = 0; i < 15; i+=4){
            int x = i;
            int y = 25-x/4*7;
            int z = 100 - x - y;
            System.out.printf("%d %d %d\r\n",x,y,z);
        }
发表于 2022-07-07 00:02:41 回复(0)
居然才打败百分之七,服了
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        sc.nextInt();
        /*
        0 25 75
        4 18 78
        8 11 81
        12 4 84
         */
        System.out.println("0 25 75");
        System.out.println("4 18 78");
        System.out.println("8 11 81");
        System.out.println("12 4 84");
    }
}


发表于 2022-06-27 23:47:46 回复(4)
import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            int x = 0;
            int y = 0;
            int z = 0;
            for (x = 0; x <= 20; x++) {
                for (y = 0; y <= 33; y++) {
                    for (z = 0; z <= 99; z++) {
                        if (x + y + z == 100 && x * 5 + y * 3 + z / 3 == 100) {
                            if(z != 0 && z%3 ==0){
                                System.out.println(x + " " + y + " " + z + " ");
                            }
                        }

                    }
                }
            }
        }
    }
}
发表于 2022-06-26 15:22:57 回复(0)
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        //平均每只鸡1块
        //买一只公鸡需要6只小鸡
        //买一只母鸡需要3只小鸡
        for (int i = 0; i < 100 / 7; i++) {//公鸡数量
            if ((100 - 7 * i) % 4 == 0) {
                int y = (100 - 7 * i) / 4;//母鸡数量
                int z = 100 - i - y;//小鸡数量
                System.out.println(i + " " + y + " " + z);
            }
        }
    }
}
发表于 2022-06-19 18:06:25 回复(0)