2013年11月8日 星期五

[USACO] 2-1-2 Ordered Fractions


/*
ID: 551100k1
LANG: C++
TASK: frac1
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct P{
    int x,y;
};
vector<P> vec;
int gcd(int a,int b){
    if(a%b==0) return b;
    return gcd(b,a%b);
}
int cmp(P a,P b){
    if(a.x*b.y!=a.y*b.x);
        return a.x*b.y<a.y*b.x;
    return a.x<b.x;
}
int main(){
    freopen("frac1.in","r",stdin);
    freopen("frac1.out","w",stdout);
    int n,i,j;
    scanf("%d",&n);
    vec.push_back((P){0,1});
    vec.push_back((P){1,1});
    for(i=1;i<n;i++){
        for(j=i+1;j<=n;j++)
            if(gcd(i,j)==1)
        vec.push_back((P){i,j});
    }
    sort(vec.begin(),vec.end(),cmp);
    for(i=0;i<vec.size();i++)
        printf("%d/%d\n",vec[i].x,vec[i].y);
}

沒有留言:

張貼留言