看看这个结果是你想要的吗?
程序代码:
/*文件名: BuyEggs.cpp*/
/*用一百元买一百个蛋,鸡蛋一毛一个,鸭蛋一块五一个,鹅蛋三块一个。请问鸡、鸭、鹅蛋各买多少个*/
#include <iostream.h>
#include <iomanip.h>
/*定义价格*/
#define PRICE_EGG 0.1 /*鸡蛋价格*/
#define PRICE_DUCK_EGG 1.5 /*鸭蛋价格*/
#define PRICE_GOOSE_EGG 3.0 /*鹅蛋价格*/
/*现有金额*/
#define MONEY 100.0
double TotalMoney( int, int, int); /*统计金额*/
int main()
{
double money = MONEY;
int count_egg = 0, count_duck_egg = 0, count_goose_egg = 0;
int count_max_eggs = ( int)( money / PRICE_EGG);
int count_max_duck_eggs = ( int)( money / PRICE_DUCK_EGG);
int count_max_goose_eggs = ( int)( money / PRICE_GOOSE_EGG);
//cout << count_max_eggs << " " << count_max_duck_eggs << " " << count_max_goose_eggs << endl; /*打印可以买到的鸡蛋数量*/
for ( ; count_egg < count_max_eggs; count_egg++)
{
for ( count_duck_egg = 0; count_duck_egg < count_max_duck_eggs; count_duck_egg++)
{
for ( count_goose_egg = 0; count_goose_egg < count_max_goose_eggs; count_goose_egg++)
{
if ( TotalMoney( count_egg, count_duck_egg, count_goose_egg) == MONEY)
{
cout << "鸡蛋 " << setw(3) << count_egg << " 个,"
<< "鸭蛋 " << setw(3) << count_duck_egg << "个,"
<< "鹅蛋 " << setw(3) << count_goose_egg << "个" << endl;
}
}
}
}
}
double TotalMoney( int eggs, int duck_eggs, int goose_eggs)
{
double total = PRICE_EGG * eggs + PRICE_DUCK_EGG * duck_eggs + PRICE_GOOSE_EGG * goose_eggs;
return total;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册