张丘建《算经》中提出“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、母、雏各几何?
分析:coks+hens+chicks=100
5*coks+3*hens+chicks/3=100
其中:0<=cocks<=19 0<=hens<=33 0<=chicks<=100
思路:一次取cocks的值域中的值,然后求其余两数,看是否合乎题意
用计算机语言写出程序
include "stdio.h"
void main()
{int cocks=0,hens,chicks;
while(cocks<=19)
{hens=0;
while(hens<=33)
{chicks=100-cocks-hens;
if(5*cocks+3*hens+chicks/3==100)
printf("cocks=%d,hens=%d,chicks=%d\n",cocks,hens,chicks);
hens++;
}
cocks++ ;
}
}