中国古代数学家张丘建在他的算经中提出了一个著名的百钱买百鸡的问题,鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问翁母雏各几何?
#include <stdio.h> #include <math.h> int main(void) { int cock = 0; int hen = 0; int chick = 0; int count = 0; int cock_price[2] = {1,5}; int hen_price[2] = {1,3}; int chick_price[2] = {3,1}; int money = 100; int max_cock,max_hen,max_chick,i,n,m,pay; max_cock = floor(money/cock_price[1])*cock_price[0]; max_hen = floor (money/hen_price[1])*hen_price[0]; max_chick = floor(money/chick_price[1])*chick_price[0]; for(i=0;i<=max_cock;i++) { for(n=0;n<=max_hen;n++) { for(m=0;m<=max_chick;m++) { if(m%3 != 0) continue; count = i + n +m; if(count != 100) continue; pay = i/cock_price[0]*cock_price[1] + n/hen_price[0]*hen_price[1] + m/chick_price[0]*chick_price[1]; if(pay == money){ printf("cock=%d,hen=%d,chick=%d \n",i,n,m); } } } } return 0; }