zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    哈希表(hash)
    并查集
    trie树(字典树)
    单调队列(滑动窗口)
    单调栈
    用数组实现栈与队列
    数组实现双链表
    数组实现单链表
    区间合并
    离散化
  • 原文地址:https://www.cnblogs.com/philippica/p/4319442.html
Copyright © 2011-2022 走看看