百鸡问题-小记

最近在看《数学与人类文明》,挺有感悟的,中学以前学的知识都是几百年以前古人得到的结果,当然可能没有今天数学的那么严谨,然而数学与社会的发展是离不开的,不论是西方的还是中国古代的,溯本追源,这是在课堂中不能学到的,书中提到了——百鸡问题,古人也有记载,如今,计算机发展快速,既然学了C语言,试试吧。

/*
百鸡问题
来源《数学与人类文明》蔡天新著,第90页所提到的《张邱建算经》最后一道题目:
公鸡每只值5文钱,母鸡每只值3文钱,而3只小鸡值1文钱。用100文钱买100只鸡,
问:这100只鸡中,公鸡、母鸡和小鸡各有多少只?

1--->x+y+z =100
2--->5x+3y+(1/3)z =100

x,y,z are all positive integral.
*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
	unsigned int x=0;
	unsigned int y=0;
	unsigned int z=0;

	unsigned int solve_num=0;

	printf("Folleing are possible plans to buy 100 fowls with 100 Yuan.\n");

	for(x=0; x<=20; x++)	//traversal cock
	{
		for(y=0; y<=33; y++)	//traversal hen
		{
			z=100-x-y;
			//put z%3==0 first
			if(z%3==0 && (5*x+3*y+z/3)==100)
			{
				solve_num++;
				printf("%d cock = %d, hen = %d, chick = %d\n",solve_num, x, y, z);
			}
		}
	}

	return 0;
}

运行结果:
Folleing are possible plans to buy 100 fowls with 100 Yuan.
1 cock = 0, hen = 25, chick = 75
2 cock = 4, hen = 18, chick = 78
3 cock = 8, hen = 11, chick = 81
4 cock = 12, hen = 4, chick = 84

posted @ 2014-05-02 12:01  wdliming  阅读(187)  评论(0编辑  收藏  举报