问题:已知公鸡5元一只,母鸡3元一只,小鸡1元三只,用100元钱买100只鸡,问公鸡,母鸡,小鸡各多少只?
C++实现
#include<iostream>
using namespace std;
void Chicken()
{
int x,y,z;
int count=0;
for(x=0;x<=20;x++)
{
for(y=0;y<=33;y++)
{
z=100-x-y;
if((z%3==0)&&(5*x+3*y+z/3==100))
{
count++;
cout<<"公鸡:"<<x<<"母鸡:"<<y<<"小鸡:"<<z<<endl;
}
}
}
if(count==0)
cout<<"问题无解:"<<endl;
}
int main()
{
Chicken();
}
Java实现
public class Main {
static void Chicken()
{
int x,y,z;
int count=0;
for(x=0;x<=20;x++)
{
for(y=0;y<=33;y++)
{
z=100-x-y;
if((z%3)==0&&(5*x+3*y+z/3)==100)
{
count++;
System.out.println("公鸡:"+x+"母鸡:"+y+"小鸡:"+z);
}
}
}
}
public static void main(String args[])
{
Chicken();
}
}