7-5 求解马克思手稿中的数学题(分数 10)
作者 黄洪艺 单位 厦门大学
求解马克思手稿中的数学题:
有30个人,在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,问男人、女人和小孩各有几人?(男人、女人和小孩都至少有1人)
输入格式:
无
输出格式:
按样例格式输出,每行一个组合,中间用逗号分隔
先放一下我的答案:
int main()
{
int man,woman,child;
const int count=50;
for (man=1;man*3<50;man++){
for(woman=1;woman*2<50;woman++){
for(child=1;child<50;child++){
if(man*3+woman*2+child==50&&man+woman+child==30){
printf("man=%d,woman=%d,child=%d\n",man,woman,child);
}
}
}
}
return 0;
}
这里便是利用了for循环来进行编写程序,因为人数30,总金额50我们是确定的。(即已知循环次数用for循环语句编写更好)
反思:那我们如何只得到一种答案呢?
如果我们仅仅在循环中加入break:
int main()
{
int man,woman,child;
const int count=50;
for (man=1;man*3<50;man++){
for(woman=1;woman*2<50;woman++){
for(child=1;child<50;child++){
if(man*3+woman*2+child==50&&man+woman+child==30){
printf("man=%d,woman=%d,child=%d\n",man,woman,child);
break;//在这里加入break跳出循环
}
}
}
}
return 0;
}
那我们会在终端得到以下输出:
可见并没有只输出一种结果。
那我们在每一层for中都加入break呢?
int main()
{
int man,woman,child;
const int count=50;
for (man=1;man*3<50;man++){
for(woman=1;woman*2<50;woman++){
for(child=1;child<50;child++){
if(man*3+woman*2+child==50&&man+woman+child==30){
printf("man=%d,woman=%d,child=%d\n",man,woman,child);
break;
}
}
break;
}
break;
}
return 0;
}
答案如下,显然不对,直接在每一层加break并没有输出情况:
说明我们的break执行需要条件,而判断条件显然要用到if语句(如下):
int main()
{
int man,woman,child;
const int count=50;
int i=0;//这里利用if1/0的判断进行break是否进行判断
for (man=1;man*3<50;man++){
for(woman=1;woman*2<50;woman++){
for(child=1;child<50;child++){
if(man*3+woman*2+child==50&&man+woman+child==30){
printf("man=%d,woman=%d,child=%d\n",man,woman,child);
i=1;
break;
}
}
if(i)break;
}
if(i)break;
}
return 0;
}
我们得到了想要的答案:
我们在这里再使用另一个方法:goto语句
int main()
{
int man,woman,child;
const int count=50;
for (man=1;man*3<50;man++){
for(woman=1;woman*2<50;woman++){
for(child=1;child<50;child++){
if(man*3+woman*2+child==50&&man+woman+child==30){
printf("man=%d,woman=%d,child=%d\n",man,woman,child);
goto exit;//这里exit相当于一个门牌
}
}
}
}
exit: //进入这个门就传送到了“exit门”的另一面
return 0;
}
一样能得到想要的答案。
所以goto语句相当于一个传送门,它可以使程序跳跃至特定的位置运行