首页 > 试题广场 >

百鸡问题

[编程题]百鸡问题
  • 热度指数:41962 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。
(本题没有测试数据,
int main(){}
就能通过
真·本地过了就是过了)


输入描述:
    测试数据有多组,输入n。


输出描述:
    对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。
示例1

输入

40

输出

x=0,y=0,z=100
x=0,y=1,z=99
x=0,y=2,z=98
x=1,y=0,z=99
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int n;
        int x=0,y=0;
        double z=100;
        int max =0;
        double total=5*x+3*y+Math.ceil(z/3);
        while(scanner.hasNext())
        {
            n = scanner.nextInt();
            while(total<=n)
            {
                z--;x++;
                total = 5*x+3*y+z/3;
            }
            max=x;
            total = 100/3; y=0;z=100;
            for(x=0;x<max;x++)
            {
                z = 100-x-y;
                total = 5*x + y*3 +Math.ceil(z/3);
                while(total<=n)
                {
                    System.out.println("x="+x+",y="+y+",z="+(int)z);
                    z--;y++;
                    total=5*x+3*y+Math.ceil(z/3);
                }
                y=0;
            }
        }
        
    }
}

发表于 2016-12-15 16:14:57 回复(0)
这道题有点彩的地方就是解决小鸡1/3元每只的问题:
(x*5*3+y*3*3+z*1<=n*3)
发表于 2018-01-19 00:02:12 回复(10)
#include <stdio.h>
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
            for(int i=0;i<=100;i++){
                for(int j=0;j<=100-i;j++){
                    int k=100-i-j;
                    if(15*i+9*j+1*k<=3*n)
                    printf("x=%d,y=%d,z=%d\n",i,j,k);
                }
            }
    }
} 

发表于 2018-03-18 15:37:03 回复(1)
x * 5 + y * 3 + z *0.333 <= n也通过了,没多想就贴合实际写的。
无脑三重循环怪我喽
#include<iostream>
#include<cstring>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int x = 0; x <=100; x++) {
		for (int y = 0; y <=100 - x; y++) {
			for (int z = 0; z <=100 - x - y; z++) {
				if ((x * 5 + y * 3 + z *0.333 <= n)&&(x+y+z==100))
					cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
			}
		}
	}
	return 0;
}

发表于 2020-02-15 18:16:06 回复(4)
#include<stdio.h>//1.分数用1.0/3直接表示
#define N 100    //2.循环优化x<=n/5;y<=n/3;z<=3*n
int main()
{
    int x,y,z,n;
    scanf("%d",&n);
    for(x=0;x<=n/5;x++)
        for(y=0;y<=n/3;y++)
            for(z=0;z<=n*3;z++)
                if(x+y+z==N&&5*x+3*y+1.0/3*z<=n)
                    printf("x=%d,y=%d,z=%d\n",x,y,z);
}

编辑于 2020-04-07 21:45:55 回复(0)
//点赞最多的解决double的问题真的是不错的方式,但其实用这个方法也不用存double
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int x,y;
        for(int x=0;x<100;x++)
            for(int y=0;y<100;y++){
                if(x*5+y*3+(100-x-y)*((double)1/3)<=n)
                    cout<<"x="<<x<<",y="<<y<<",z="<<100-x-y<<endl;
                else
                    break;
            }
    }
    return 0;
}

发表于 2020-01-12 21:24:29 回复(0)
因为只有z涉及到小数,所以把z分成是不是3的倍数,是的话花的钱为z/3,不是的话算作z/3+1
#include <iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        for(int x=0;x<=100;x++)
        {
            for(int y=0;y<=100-x;y++)
            {
                int z=100-y-x;
                if(5*x+3*y+((z%3)?z/3+1:z/3)<=n) cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl; 
            }
        }
    }
    return 0;
}
发表于 2022-01-02 12:45:51 回复(0)
#include <stdio.h>
int main()
{
    int i, j,n,left,a;
    while(scanf("%d",&n) == 1)
    {
            //价格都假定为原来的三倍,n也乘3,如果全买小鸡的钱都不够就进行下次循环
        if(3 * n < 100)
            continue;
                //否则打印信息
        printf("x=0,y=0,z=100\n");
                //left是假设全买小鸡剩下的钱,每再多8元,就可以换一只9元的小鸡
        left = 3 * n - 100;
    if(left < 8)
        continue;

    for(i = 1; i <= left / 8 && i <= 100;i++)
        printf("x=0,y=%d,z=%d\n",i,100 - i);
        //每多14元就可以换一只15元的小鸡
    if(left < 14)
        continue;
    for(i = 1;i <= left / 14 && i <= 100;i++)
    {
        a = left - i*14;
        for(j = 0;j <= a/8 && i + j <= 100;j++)
            printf("x=%d,y=%d,z=%d\n",i,j,100-i-j);
    }

    }
}


编辑于 2021-02-14 20:54:46 回复(0)
#include<stdio.h>
int main (){//the shorter,the better.
    int n,i,j;
    for(;~scanf("%d",&n);)
        for (i = 0; i <= 100; i++)
            for (j = 0; j <= 100-i;i*15+9*j+100-i-j > 3*n?:printf("x=%d,y=%d,z=%d\n",i,j,100-i-j),j++);
}

发表于 2018-01-14 15:25:14 回复(2)
枚举法暴力求解,在for循环时取100和n元钱能买到的每一类鸡的最小值来限制循环次数

#include<iostream>
using namespace std;

const int BigChickenPrice = 5;
const int SmallChickenPrice = 3;
const double AnotherSmallChickenPrice = 1.0 / 3.0;

int Min(double a, double b) {
	return(a < b) ? a : b;
}

int main() {
	int n = 0;
	cin >> n;
	for (int i = 0; i <= Min(n / BigChickenPrice,100.0); ++i) {
		for (int j = 0; j <= Min(n / SmallChickenPrice,100.0); ++j) {
			for (int k = 0; k <= Min(n / AnotherSmallChickenPrice,100.0); ++k) {
				if (5.0 * i + 3.0 * j + (1.0 / 3.0) * k <= n && i + j + k == 100) {
					cout <<"x=" << i <<"," << "y=" << j << "," << "z=" << k << endl;
				}
			}
		}
	}
	return 0;
}

发表于 2024-01-04 14:39:09 回复(0)
Java 解法一:
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int x = 0; x < 101; x++) {
            for (int y = 0; y < 101; y++) {
                for (int z = 0; z < 101; z++) {
                    if (x+y+z==100&&x*5+y*3+z/3<=n)
                        System.out.println("x="+x+",y="+y+",z="+z);
                }
            }
        }
    }
}

解法二
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int x = 0; x <= 100; x++) {
            for (int y = 0; y <= 100-x; y++) {
                int z=100 -y-x;
                if (x*5+y*3+z/3<=n)
                    System.out.println("x="+x+",y="+y+",z="+z);
            }
        }
    }
}



发表于 2020-03-06 12:05:09 回复(0)
#include <iostream>
(720)#include <cstdio>

using namespace std;

int main() {
	int n;
	int x, y, z;
	while (scanf("%d", &n) != EOF) {
		for ( x = 0; x <= n/5; x++) {
			for ( y = 0; y <= n/3; y++) {
				z = 100 - x - y;
				if (x * 5 * 3 + y * 3 * 3 + z <= n * 3) {
					printf("x=%d,y=%d,z=%d\n", x, y, z);
				}
			}
		}
	}
	return 0;
}

发表于 2020-02-24 00:18:45 回复(0)
#include <iostream>
using namespace std;
int main()
{
    int n,k;
    cin>>n;
    for(int i=0;i<=100;i++)
        for(int j=0;j<=100-i;j++)
        {
            k=100-i-j;
            if((5*i+3*j+k/3)<(n)) //根本不用管 把z的小数去了其实不影响结果 :)滑稽
                cout<<"x="<<i<<",y="<<j<<",z="<<k<<endl;
        }
    return 0;                  
 } 
发表于 2020-02-06 21:11:24 回复(2)
#include<bits/stdc++.h>
int main(){
    int n;
    while(scanf("%d",&n)!=EOF)
        for(int i=0;i<n;i++)
            for(int j=0;j<100-i;j++)
                if((5*i+3*j+(100-i-j)/3)<=n)
                    printf("x=%d,y=%d,z=%d\n",i,j,100-i-j);
}

编辑于 2019-03-04 11:21:28 回复(1)

并不是无脑循环,循环次数较少,这循环次数差不多是最少了,还能再优化吗?

def findHundredChicken(x,y,z,price):   #递归增加小鸡,减少小小鸡,输出
    if 5*x+3*y+1/3*z <= price:         #价格超出后不再递归
        print("x=%d,y=%d,z=%d"%(x,y,z))
        findHundredChicken(x,y+1,z-1,price)

try:
    while True:
        num = int(input())
        for i in range(101):    #在这里循环到大鸡最多能买的个数
            if (num-i*5)*3<100-i:    
                break
            findHundredChicken(i,0,100-i,num)        #始终保持百鸡
except Exception:
    pass
编辑于 2018-09-18 15:00:46 回复(0)
#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    int x = 0;
    int y = 0;
    int z = 0;
    for(x = 0;x<=100;x++){
        for(y = 0;y<=100;y++){
            for(z = 0;z<=100;z++){
                if(x*5*3+y*3*3+z <= n*3 && x+y+z==100){
                    cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
                }
            }
        }
    }
}
编辑于 2024-02-14 23:09:49 回复(0)
#include <iostream>
using namespace std;

int main() {
    float n;
    while (cin >> n) {
        for (int x = 0; x <= 100; x++) {
            for (int y = 0; y <= 100 - x; y++) {
                int z = 100 - x - y;
                if (x * 5 + y * 3 + z * 1.0 / 3 <= n) {
                    cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
                }
            }
        }
    }
    return 0;
}

发表于 2024-02-02 10:48:14 回复(0)
#include <cstdio>  using namespace std; int main(){ int n,x,y,z;
    scanf("%d",&n); for(x=0;x<=n/5;x++){ for(y=0;y<=n/3;y++){ for(z=0;z<=n*3;z++) if(x+y+z==100 && 15*x + 9*y + z <= 3*n)
               {
                   printf("%d %d %d\n",x,y,z);
               }
       }
   } return 0 ;
}

编辑于 2024-01-22 17:37:33 回复(0)
#include<stdio.h>
using namespace std;

int main(){
    double n, x, y, z;
    scanf("%lf", &n);
    for(x = 0; (x <= 100) && (x <= n / 5); x++){
        for(y = 0; (y <= 100) && (y <= n / 3); y++){
            for(z = 0; (z <= 100) && (z <= n * 3); z++){
                if((x + y + z == 100) && (x * 5 + y * 3 + z / 3 <= n)){
                	printf("x=%d,y=%d,z=%d\n", (int)x, (int)y, (int)z);
				}
            }
        }
    }

    return 0;
}

发表于 2024-01-22 17:28:42 回复(0)
#include<cstdio>
int main()
{
    int n;
    int x,y,z;
//    z=100-x-y;
    while(scanf("%d",&n)!=EOF)
    {
        for(x=0;x<=100;x++)//x的范围
        {
            for(y=0;y<=100;y++)
            {
                z=100-x-y;//当x取100时,会出现z<0的情况,于是在下面的if判断中将z<0的值舍弃
                //主要是1/3搞不好会变为0
                //所以想着全部×3 避免1/3的出现
                if(z>=0 && 15*x+9*y+z<=3*n)//花费的钱数,z>=0
                    printf("x=%d,y=%d,z=%d\n",x,y,z);
                else
                    break;
            }
        }
    }
    return 0;
}


发表于 2023-04-14 09:06:09 回复(0)

问题信息

难度:
108条回答 12098浏览

热门推荐

通过挑战的用户

查看代码