http://ace.delos.com/usacoprob2?a=RTmsdUP0y2Y&S=frac1
//一个非常巧妙的方式输出了所有大小排序既约分数
//注意其输出顺序的对应
/*
ID: lvxiaol3
PROG: frac1
LANG: C++
*/
/*
0/1 1/1
1/2
1/3 2/3
1/4 2/5 3/5 3/4
1/5 2/7 3/8 3/7 4/7 5/8 5/7 4/5
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int n;
FILE *fout;
/* print the fractions of denominator <= n between n1/d1 and n2/d2 */
void genfrac(int n1, int d1, int n2, int d2)
{
if(d1+d2 > n)
/* cut off recursion */
return;
genfrac(n1,d1, n1+n2,d1+d2);
fprintf(fout, "%d/%d/n", n1+n2, d1+d2);
genfrac(n1+n2,d1+d2, n2,d2);
}
void main(void)
{
FILE *fin;
fin = fopen("frac1.in", "r");
fout = fopen("frac1.out", "w");
assert(fin != NULL && fout != NULL);
fscanf(fin, "%d", &n);
fprintf(fout, "0/1/n");
genfrac(0,1, 1,1);
fprintf(fout, "1/1/n");
}
//问题收获是记住此种生成既约分数的方法!以后快速生成