此为原创,如有雷同,纯属意外,毕竟方法就这几种
设X,Y,Z分别代表男人,女人,小孩的人数,测程序为
#include <stdio.h>
#include <math.h>
int main()
{
int x,y,z,count = 0;
printf (" men women children\n");
for (x = 0;x < 17;x++)
for (y = 0;y <= 30 - x;y++)
for (z = 0;z <= 30 - x - y;z++)
{
if ((3*x + 2*y + z == 50)&&(x + y + z == 30))
printf (" %2d: %2d %2d %2d\n",++count,x,y,z);
}
return 0;
}
[root@localhost 41-45]# vim 41.c
[root@localhost 41-45]# gcc 41.c -o 41
[root@localhost 41-45]# ./41
men women children
1: 0 20 10
2: 1 18 11
3: 2 16 12
4: 3 14 13
5: 4 12 14
6: 5 10 15
7: 6 8 16
8: 7 6 17
9: 8 4 18
10: 9 2 19
11: 10 0 20
结果是这个