[儿童节]此小程序纪念我童年看爸爸的书的时光.

小时候不厌其烦的手算繁分数.
今天写一个小程序自动计算
算是小纪念吧.
42897 18644

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

void ChildLjw( int ljw[], int i, int j, int *a, int *b )
{ 
	int x, y;

	if( i == j )
	{
		*a = 1;
		*b = ljw[i];
	}
	else
	{
		ChildLjw( ljw, i+1, j, &x, &y );
		*a = y;
		*b = ljw[i]*y + x;
	}
}

int main()
{
	int ljw[1000] = {5,6,7,8};
	int a, b, c = 1;
	int i, ljwn;

	while( scanf("%d%d", &a, &b ) != EOF)
	{
		i = 0;
		while( b )
		{
			c = a / b;
			ljw[i] = c;
			i++;
			c = a - b * c;
			a = b;
			b = c;
		}
		ljwn = i;

		printf("%d/1n", ljw[0]);
		for( i = 0; i < ljwn-1; i++ )
		{
			ChildLjw( ljw,1,1+i, &a, &b);
			a = ljw[0] * b + a;
				printf("%d/%dn", a,b );
		}
	}

}

 


本文链接

回复