AmazingCounters.com

USACO Ordered Fractions

题目大意:求一个n的farey序列

思路:懒得麻烦的推导和公式了,暴力压倒一切

/*{
ID:a4298442
PROB:frac1
LANG:C++
}
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<fstream>
#define maxn 160*160+10
using namespace std;
ifstream fin("frac1.in");
ofstream fout("frac1.out");
struct T
{
    int up;int down;double val;
}a[maxn];
int cmp(T x,T y)
{
    return x.val<y.val;
}
int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    int n,h=0;
    fin>>n;
    fout<<"0/1"<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)if(gcd(i,j)==1)
        {
            a[++h].up=i;a[h].down=j;
            a[h].val=(double)1.0*i/j;
        }
    }
    sort(a+1,a+1+h,cmp);
    for(int i=1;i<=h;i++)
    {
        fout<<a[i].up<<"/"<<a[i].down<<endl;
    }
    return 0;
}

 

posted @ 2015-03-06 22:51  philippica  阅读(99)  评论(0编辑  收藏  举报