zzulioj 1075: 聚餐人数统计

题目描述
马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计n个人,在一家饭店里吃饭,共花了cost先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几个。
输入
输入两个正整数,表示人数n和花费cost。
输出
若问题有解,则输出所有解,每行输出三个数据,代表男人、女人和小孩的人数,用空格分隔;若问题无解,则输出“No answer"。
样例输入 Copy
30 50
样例输出 Copy
0 20 10
1 18 11
2 16 12
3 14 13
4 12 14
5 10 15
6 8 16
7 6 17
8 4 18
9 2 19
10 0 20

#include<stdio.h>
int main()
{
    
    
	int a, b, c, n, cost, x=0;
	scanf("%d%d",&n,&cost);
	for(a=0;a<=cost/3;a++)
	  for(b=0;b<=cost/2;b++)
	    for(c=0;c<=cost;c++)
	    if(3*a+2*b+c==cost)
	      if(a+b+c==n)
	          {
    
    
	          	printf("%d %d %d\n",a, b, c);
	          	x=1;
			  }
	if(x!=1)
	  printf("No answer\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_53024529/article/details/113354936