题目:
公鸡每只五元,母鸡每只三元,小鸡一元三只
用一百元买一百只鸡,可以买多少只公鸡,多少只母鸡,多少只小鸡
a.第一种解法,用for语句
int main()
{
float sump, sumc;
int i, j, k;
for (i = 0; i <= 20; i++)
{
for (j = 0; j <= 33; j++)
{
for (k = 0; k <= 100 - i - j; k++)
{
sump = k / 3.0 + j * 3.0 + 5.0 * i;
sumc = i + j + k;
if (sump == 100.0&&sumc==100)
{
printf("公鸡、母鸡、小鸡有:%d %d %d\n", i, j, k);
}
}
}
}
return 0;
}
b.第二种解法,用while语句
int main()
{
int i = 0;
int j = 0;
int k = 0;
while (i<=20)
{
j = 0;
while (j <= 33)
{
k = 100 - i - j;
if (5.0 * i + 3.0* j + k / 3.0 == 100)
printf("公鸡、母鸡、小鸡有:%d %d %d\n", i, j, k);
j++;
}
i++;
}
return 0;
}