马克思手稿中有一道趣味数学题:有30个人,其中有男人、女人和小孩,在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,问男人、女人和小孩各有几人?
#include
int main()
{
int man_num, woman_num;
for(man_num = 0; man_num < 50/3; man_num++)
for(woman_num = 0; woman_num < (50-3*man_num)/2; woman_num++) {
if(man_num*3+woman_num*2+(30-man_num-woman_num)*1 == 50) {
printf("man_num = %2d, woman_num = %2d, child_num = %2d\n", man_num, woman_num, 30-man_num-woman_num);
}
}
return 0;
}
程序在linux下的运行结果截图: