【USACO 2.1】Ordered Fractions

2020-06-02 11:01:39 浏览数 (1)

TASK: 直接枚举,约分,排序,去重

代码语言:javascript复制
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
    int nu,deno;
    double v;
}a[40000];
int n,cnt;
int cmp(node a,node b){
    return a.v<b.v;
}
int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
int main(){
    freopen("frac1.in","r",stdin);
    freopen("frac1.out","w",stdout);
    scanf("%d",&n);
    for(int i=0;i<=n;i  )
        for(int j=max(i,1);j<=n;j  ){
            int g=gcd(i,j);
            a[cnt  ]=(node){i/g,j/g,i*1.0/j};
        }
    sort(a,a cnt,cmp);
    for(int i=0;i<cnt;i  ){
        if(i==0||a[i].v-a[i-1].v>1e-6||a[i].v-a[i-1].v<-1e-6)
        printf("%d/%dn",a[i].nu,a[i].deno);
    }
    return 0;
}

0 人点赞