Ordered Fractions

链接

分析:遍历一下,求个gcd即可,最后按照ans排序并去重

 1 /*
 2     PROB:frac1
 3     ID:wanghan
 4     LANG:C++
 5 */
 6 #include "iostream"
 7 #include "cstdio"
 8 #include "cstring"
 9 #include "string"
10 #include "algorithm"
11 #include "vector"
12 using namespace std;
13 int n;
14 int gcd(int a,int b){
15     if(b==0)  return a;
16     return gcd(b,a%b);
17 }
18 struct Node{
19     int x,y;
20     double ans;
21 };
22 vector<Node> p;
23 bool cmp(Node a,Node b){
24     return a.ans<b.ans;
25 }
26 int main()
27 {
28     freopen("frac1.in","r",stdin);
29     freopen("frac1.out","w",stdout);
30     cin>>n;
31     for(int i=0;i<=n;i++){
32         for(int j=min(n,i+1);j<=n;j++){
33             int x,y;
34             x=i/gcd(i,j),y=j/gcd(i,j);
35             Node t;
36             t.x=x,t.y=y,t.ans=(double)x/(double)y;
37             p.push_back(t);
38         }
39     }
40     sort(p.begin(),p.end(),cmp);
41     cout<<p[0].x<<"/"<<p[0].y<<endl;
42     for(int i=1;i<p.size();i++){
43         if(p[i].x==p[i-1].x&&(p[i].y==p[i-1].y)) continue;
44         cout<<p[i].x<<"/"<<p[i].y<<endl;
45     }
46     return 0;
47 }
View Code

 

posted @ 2017-07-05 00:06  wolf940509  阅读(188)  评论(0编辑  收藏  举报