首页 > 试题广场 >

百钱买百鸡问题

[编程题]百钱买百鸡问题
  • 热度指数:114207 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
公元五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
现要求你打印出所有花一百元买一百只鸡的方式。

输入描述:

输入任何一个整数,即可运行程序。



输出描述:

 输出有数行,每行三个整数,分别代表鸡翁,母鸡,鸡雏的数量

示例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)  {
        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 回复(2)
穷举
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)
可能对于初学者来说,我这种方法比较通俗易懂一些...
发表于 2022-06-07 19:39:47 回复(0)
import java.util.*;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息,这不就一个解
public class Main {
    public static void main(String[] args) {
            System.out.println(0 + " " + 25  + " " + 75 
                     + "\n" + 4  + " " + 18  + " " + 78 
                     + "\n" + 8 + " " + 11  + " " + 81 
                     + "\n" + 12 + " " + 4  + " " + 84 );
    }

发表于 2022-06-02 13:58:40 回复(0)
import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int x = 0, y = 0, z = 0;
        for (int i = 0; i <= 25; i++) {
            x = 4 * i;
            y = 25 - 7 * i;
            z = 75 + 3 * i;
            if (y >= 0 && z <= 100) {
                System.out.println(x + " " + y + " " + z);
            }
        }
    }
}
发表于 2022-06-01 11:59:58 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int jp = sc.nextInt();
            int x,y;
            float z;
            for(x = 0;x <= 15;x++){
                for(y = 0;y <= 25;y++){
                    z = 100-x-y;
                    if((5*x+3*y+z/3)==100){
                        System.out.println(x+" "+y+" "+(int)z);
                    }
                }
            }
        }
    }
}

发表于 2022-05-29 00:30:43 回复(0)

问题信息

难度:
56条回答 34441浏览

热门推荐

通过挑战的用户

查看代码