//公鸡5块钱一只、母鸡3块钱一只、小鸡一块钱三只;用100块钱买100个鸡;
//x+y+z=100鸡数
//5X+3y+z/3=100钱
//x=(1,20)
//y=(1,33)
//z=(3,99)
#include <iostream>
using namespace std;
void main()
{
int x,y,z;
for(x=1;x<=20;x++)
{
for(y=1;y<=33;y++)
{
for(z=3;z<=99;z++)
{
if((x+y+z==100)&&(5*x+3*y+z/3==100)&&z%3==0)
{
cout<<"x:"<<x<<"\t"<<"y:"<<y<<"\t"<<"z:"<<z<<endl;
}
}
}
}
}
或则用双重循环也行:
#include <iostream>
using namespace std;
void main()
{
int x,y,z;
for(x=1;x<=20;x++)
{
for(y=1;y<=33;y++)
{
z=100-x-y;
if((x+y+z==100)&&(5*x+3*y+z/3==100)&&(z%3==0))
{
cout<<"x:"<<x<<"\t"<<"y:"<<y<<"\t"<<"z:"<<z<<endl;
}
}
}
}
[
本帖最后由 Aidoneus 于 2012-9-5 13:21 编辑 ]