分享一道作业题221029

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语句相当于一个传送门,它可以使程序跳跃至特定的位置运行

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值