【USACO】Ordered Fraction

有序既约分数

原理是法雷数列

/*
ID :
LANG: C++11
TASK: frac1
 */

#include 
#include 
#include 
#include 

using namespace std;

int N;

void farey(int x1, int y1, int x2, int y2){
    int midx = x1 + x2;
    int midy = y1 + y2;
    if (midy > N){
        return;
    }
    farey(x1, y1, midx, midy);
    printf("%d/%d\n", midx, midy);
    farey(midx, midy, x2, y2);

}


int main()
{
    freopen("frac1.in", "r", stdin);
    freopen("frac1.out", "w", stdout);

    cin >> N;
    cout << "0/1\n";
    farey(0, 1, 1, 1);
    cout << "1/1\n";
    return 0;
}

你可能感兴趣的:(USACO,搜索,DFS)