首页 > 试题广场 >

百钱买百鸡问题

[编程题]百钱买百鸡问题
  • 热度指数:112025 时间限制: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.*;

public class Main {
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
        	int num = sc.nextInt();
        	int n1 = 0; // 鸡翁个数
        	int n2 = 0; // 鸡母个数
        	int n3 = 3; // 鸡雏个数
        	for(n1 = 0; n1 < 20; n1++){
        		for(n2 = 0; n2 < 33; n2++){
        			for(n3 = 0; n3 < 100; n3 = n3 + 3){
        				if(n1 + n2 + n3 == 100 && 5*n1 + 3*n2 + n3/3 == 100){
        					System.out.println(n1 + " " + n2 +  " " + n3);
        				}
        			}
        		}
        	}		        	
        }
        sc.close();
    }
}

发表于 2016-08-13 17:11:27 回复(1)
本质上就是解方程,满足约束条件  5x+3y+z/3 =100 以及 x+y+z=100;其中隐含条件是z必须是3的倍数,所以 x=4z/3 -100;y=200-7z/3;只需要遍历z,每次z加3,找到满足条件(0<=x<=100且0<=y<=100)的x和y就行,不需要判定是整数,因为z是3的倍数,所以x和y必定也是整数。(没有更进一步限定x和y的取值,交给计算机做吧)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException  {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String s = "";
        while ((s = bf.readLine()) != null){
            int num = Integer.parseInt(s);
            int x,y;
            for (int z = 0 ; z<=100; z +=3){
                x = 4*z/3-100;
                y = 200-7*z/3;
                if ( (x>=0)&&(x<=100) &&(y>=0)&&(y<=100)){
                    System.out.println(x + " " + y + " " + z );
                }
            }
        }
    }
}


发表于 2020-04-26 00:47:50 回复(0)
for i in range(0,25):
    y = (100 - 7*i)//4
    if(((7*i + 4*y) == 100) and (y > 0)):
        print(str(i)+' '+str(y)+' '+str(100 - y - i))

发表于 2022-06-22 21:42:16 回复(3)

for i in range(0,20):
    for j in range(0,34):
        if isinstance(100-int(i)-int(j), int):
            if int(i)*5+int(j)*3+((100-int(i)-int(j))/3)==100:
                print("%s %s %d"%(i,j,(100-int(i)-int(j))))
发表于 2021-09-28 21:26:29 回复(0)
import java.io.*;
public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while((str=br.readLine())!=null){
            StringBuilder sb = new StringBuilder();
           
            // 设公鸡X 母鸡Y 小鸡(100-X-Y) 则 5X+3Y+1/3(100-X-Y)=100  化简得 7X+4Y=100;
            for(int i=0;i<=13;i++){
                for(int j=0;j<=25;j++){
                    if(7*i+4*j==100){
                        sb.append(i).append(" ").append(j).append(" ").append(100-i-j).append("\n");
                        break;
                    }
                }
            }
            
            // 设公鸡X 母鸡Y 小鸡(100-X-Y) 则 5X+3Y+1/3(100-X-Y)=100  化简得 7X+4Y=100;  Y=(100-7X)/4
//             for(int i=0;i<=14;i++){
//                int y = (100-7*i)/4;
//                if(7*i+4*y==100){
//                    sb.append(i).append(" ").append(y).append(" ").append(100-i-y).append("\n");
//                }
//             }
            
            System.out.print(sb.toString());
        }
    }
}

发表于 2021-09-22 16:57:18 回复(0)
import java.util.*;
public class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
//          System.out.println("0 25 75");
//          System.out.println("4 18 78");
//          System.out.println("8 11 81");
//          System.out.println("12 4 84");
//        
           for(int x = 0;x <= 3;x++){
               System.out.println((4*x) + " " + (25-7*x) + " " + (3*x+75));
           }
        }
        sc.close();
    }
}

发表于 2021-08-15 23:23:47 回复(0)
# 6x + 3y + x + y= 100
while True:
    try:
        n = int(input())
        for i in range(21):
            y = (100-7*i)/4
            if y%1 == 0 and y>0:
                print(i,int(y),int(6*i+3*y))
    except:
        break
x为公鸡个数,y为母鸡个数。一只公鸡需要6只雏鸡来平均价钱,一直母鸡需要3只雏鸡平均。
雏鸡个数为 6x + 3y
循环公鸡情况即可。
发表于 2021-07-15 17:34:16 回复(1)
一开始审题不清,居然没看到是“鸡雏三”才“值钱一”,卡了一会儿……
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<=100/5+1;++i) {
        for(int j=0;j<=(100-5*i)/3+1;++j) {
            for(int k=0;k<=(100-5*i-3*j)*3;k+=3) {
                if(5*i+3*j+k/3==100 && i+j+k==100) {
                    cout<<i<<" "<<j<<" "<<k<<endl;
                }
            }
        }
    }
    return 0;
}
发表于 2021-06-27 16:49:36 回复(0)
#include <iostream>

using namespace std;
//计算
//5x+3y+1/3z = 100
//x+y+z = 100
//得出 7x+4y = 100    0<=x<=14    0<=y<=25
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i = 0;i<=14;i++)
        {
            for(int j = 0;j<=25;j++)
            {
                if(7*i+4*j == 100)
                {
                    int tmp = 100 -i - j;
                    cout<<i<<" "<<j<<" "<<tmp<<endl;
                }
            }
        }
    }

    return 0;
}

编辑于 2020-07-12 11:30:22 回复(0)
#include<iostream>
using namespace std;

int main()
{
    int i,j,k,n;
    while(cin>>n)
    {
        for(i=0;i<20;i++)
        {
            for(j=0;j<34;j++)
            {
                k=100-i-j;
                if((k%3==0)&&(5*i+3*j+k/3==100))
                {
                    cout<<i<<" "<<j<<" "<<k<<endl;
                }
            }
        }
    }
    return 0;
}
很经典的一个问题了
发表于 2019-11-29 14:07:58 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            List<String> s = new ArrayList<String>();
            for(int i=0; i<=100; i++) {
                for(int j=0; j<=100; j++) {
                    int k = 100-i-j;
                    if(k%3==0 && 5*i+3*j+k/3==100) {
                        s.add(i+" "+j+" "+k);
                    }
                }
            }
            for(String ss : s) {
                System.out.println(ss);
            }
        }
    }
}

发表于 2018-10-08 12:50:06 回复(0)
 
#include<iostream>
using namespace std;
int main() 

    int n;
    int i=0,j=0,k=0;
    while(cin>>n)
   {
   
    for(i=0;i<=25;i+=4) 
   { 
      for(j=25;j>0;j-=7) 
        { 
            for(k=75;k<=100;k+=3) 
            { 
              if((i*5+j*3+k/3==100)&&(i+j+k==100))
                    cout<<i<<' '<<j<<' '<<k<<endl; 
           } 
       } 
    }
    }
  return 0; 

发表于 2017-07-06 10:08:24 回复(0)
三元不定方程组,消元后用一个参数计算另外另个,并且通过解应为正整数,判断结果是否符合要求。
#include<iostream>
using namespace std;
int main(){
    int input;
    while(cin>>input){
        for(int x=0;x<21;x++){
            double y=(200-14.0*x)/8;
            double z=(600+6.0*x)/8;
            if((y-(int)y)==0 && (z-(int)z)==0 && (int)z%3==0 && y>=0 && z>=0)
                cout<<x<<' '<<(int)y<<' '<<(int)z<<endl;
        }
    }
    return 0 ;
}
发表于 2017-06-07 09:17:46 回复(0)
#include<iostream>
#include<vector>
#include<tuple>
using namespace std;
int main() {
	int num = 0;
	while (cin >> num) {
		vector<tuple<int,int,int>> res;
		for (int i = 0; i <= 20; i++)
			for (int j = 0; j <= (100 - 5 * i) / 3; j++)
				for (int k = 0; k <= 3 * (100 - 5 * i - 3 * j); k += 3)
					if (i + j + k == 100 && 5 * i + 3 * j + k / 3 == 100)
						res.push_back(make_tuple(i, j, k));
		for (auto t : res)
			cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << endl;
	}
}

发表于 2017-03-18 18:49:52 回复(1)
#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<=20;++i)
            for(int j=0;j<=33;++j)
                for(int k=0;k<=300;++k)
                    if((i+j+k==100)&& (15*i+9*j+k==300) && k%3==0)
                        cout<<i<<" "<<j<<" "<<k<<endl;
    }
    return 0;
}

编辑于 2017-02-05 09:52:08 回复(0)
import java.util.Scanner;

public class Demo32 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int num = sc.nextInt();
			calculate();
		}
		sc.close();
	}
	public static void calculate(){
		for(int i=0;i<=20;i++){
			for(int j=0;j<=33;j++){
				for(int k=0;k<=300;k++){
					if(15*i+9*j+k==300&&i+j+k==100){ 
						System.out.println(i+" "+j+" "+k);
					}
				}
			}
		}
	}
}

发表于 2016-12-19 21:46:02 回复(3)

import java.util.*;

/** * 鸡翁(x)、鸡母(y)、鸡雏(z)问题是求 100 = 5x + 3y+ z/3 且 100 = x + y + z的所有可能解 * * */
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
in.nextLine();
System.out.print(getResult());
}
in.close();
}

static String getResult() {
StringBuffer buffer = new StringBuffer();
for (int x = 0; x <= 100; x++) {
for (int y = 0; y <= 100 - x; y++) {
int z = 100 - x - y;
if (z % 3 == 0 && 100 == 5 * x + 3 * y + z / 3) {
buffer.append(x).append(" ").append(y).append(" ").append(z).append("\n");
}
}
}
return buffer.toString();
}
}

发表于 2016-07-06 14:33:28 回复(0)
#include <iostream>
using namespace std;
int main()//时间复杂度是O(n)
{
int num = 0;
while (cin >> num) {
int cock = 0, hen = 0, chick = 0;
for (int k = 0; k < 4; k++) {
cock = 4 * k;
hen = 25 - 7 * k;
chick = 75 + 3 * k;
cout<<cock<<' '<<hen<<" "<<chick<<endl;
}
}
return 0;
}
发表于 2016-07-02 11:23:21 回复(3)
import sys
while True:
    try:

        string = sys.stdin.readline()
        int(string)
        
        for i in range(101):
            for j in range(101-i):
                for k in range(101-i-j):
                    if 15*i+9*j+k==300 and i+j+k==100:
                        print i,j,k
    except:
        break
#放心吧,如今你所放不下的人和事,岁月会替你轻描淡写。 


发表于 2016-07-02 10:29:36 回复(0)
老铁们,我做的对吗
import java.util.Scanner;  
public class Main { 

public static void main(String[] args) {
           Scanner sc=new Scanner(System.in);  
          while(sc.hasNext()){ 
           int num=sc.nextInt();
           System.out.println(0+" "+25+" "+75);
           System.out.println(4+" "+18+" "+78);
           System.out.println(8+" "+11+" "+81);
           System.out.println(12+" "+4+" "+84); 
                  }  }



发表于 2020-05-05 13:41:09 回复(3)