程序代码:
#include<stdio.h>
void cal(int a, int b, int c, int d, int n)
{ //将区间不断分割,直到区间(a, b)满足 x/n < a < b < (x+1)/n
//再减小n,直到满足(x+1)/n < b,然后分割、减小交替执行,
//直到n = 0。递归下一个区间cal(e, f, c, d, n);
//杨大哥好厉害!
int e, f, t, t1, t2;
if(n <= 0) return;//递归结束条件
e = a * n / b + 1;
//a / b = x / n; e = [x] + 1;//保证e / n > a / b且使 e最小
f = n;
if(e * d >= c * f){ cal(a, b, c, d, n - 1); return;}
//e / f >= c / d时,递归n - 1
for(t1 = e, t2 = f; t = t1 % t2; t1 = t2, t2 = t);
//辗转相除法求e、f最大公约数
e /= t2; f /= t2;
//以e / f为分界线,分别递归左边和右边
cal(a, b, e, f, n);
printf("%d/%d\n", e, f);
cal(e, f, c, d, n);
}
int main()
{
cal(1, 2, 2, 3, 10);
return 0;
}