Ordered Fractions
Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.
Here is the set when N = 5:
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.
PROGRAM NAME: frac1
INPUT FORMAT
One line with a single integer N.
SAMPLE INPUT (file frac1.in)
5
OUTPUT FORMAT
One fraction per line, sorted in order of magnitude.
SAMPLE OUTPUT (file frac1.out)
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
#include<stdio.h>
void pre(int x1, int x2, int y1, int y2, int n)
{
if (y1 + y2 > n)
{
return;
}
pre(x1, x1 + x2, y1, y1 + y2, n);
printf("%d/%d\n", x1 + x2, y1 + y2);
pre(x1 + x2, x2, y1 + y2, y2, n);
}
int main()
{
int n;
freopen("frac1.in", "r", stdin);
freopen("frac1.out", "w", stdout);
scanf("%d", &n);
printf("0/1\n");
pre(0, 1, 1, 1, n);
printf("1/1\n");
fclose(stdin);
fclose(stdout);
return 0;
}
(n1,n1+n2,d1,d1+d2)后关系n1/(n1+n2) < d1/(d1+d2) <式1>仍然成立,可以通过对式1两边分别乘上(n1+n2)(d1+d2)并移动项得到n1/n2 < d1/d2而得,右子树的也可以这样论证。但我不知道WHY