The test instructions is given an n, and the output is sorted by a nonnegative fraction of all the denominator less than N.
/*id:modengd1prog:frac1lang:c++*/#include <iostream> #include <stdio.h> #include <memory.h># Include <queue>using namespace Std;bool vis[261][261];//, the score after the quote may exist after numerator equal score int gcd (int x,int y) {return y= =0?X:GCD (y,x%y);} struct fra{//numerator and denominator int num1,num2; FRA (int a,int b)//constructs numerator {int C=GCD (A, b) at the same time; NUM1=A/C; NUM2=B/C; } bool Friend operator < (FRA f1,fra f2) {return f1.num1*f2.num2>f2.num1*f1.num2;//-pass compare size to sort by priority queue so yes Greater than number}};int main () {freopen ("frac1.in", "R", stdin); Freopen ("Frac1.out", "w", stdout); int N; Priority_queue<fra> Q; memset (vis,false,sizeof (VIS)); scanf ("%d", &n); if (n==0)//press into 0 Q.push (FRA (0,0)); Else Q.push (FRA (0,1)); for (int j=1;j<=n;j++)//enumeration denominator {for (int i=1;i<j;i++)//enumeration molecule {fra F=fra (i,j); if (vis[f.num1][f.num2])//If the enumerated numerator is already equal to the previous one, the continue is ignored; Vis[f.num1][f.num2]=true; Q.push (f); }} if (n!=0)//press into 1 Q.push (FRA); while (! Q.empty ())//Output {fra now=q.top (); Q.pop (); cout<<now.num1<< '/' <<now.num2<<endl; } return 0;}
Usaco Ordered Fractions