USACO Section 2.1: Prob Ordered Fractions

不知道这题为什么放图论题里。。

 1 /*

 2 ID: yingzho1

 3 LANG: C++

 4 TASK: frac1

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 

17 using namespace std;

18 

19 int paths[180][180];

20 int N;

21 

22 ifstream fin("frac1.in");

23 ofstream fout("frac1.out");

24 

25 struct node {

26     int x;

27     int y;

28     node(int a, int b) : x(a), y(b) { }

29     node() : x(0), y(0) { }

30 };

31 

32 bool cmp(const node &a, const node &b) {

33     return a.x*b.y < a.y*b.x;

34 }

35 

36 int GCD(int a, int b)

37 {

38    if (b==0) return a;

39    return GCD(b,a%b);

40 }

41 

42 int main()

43 {

44     fin >> N;

45     fout << "0/1" << endl;

46     vector<node> fraction;

47     for (int i = 1; i <= N-1; i++) {

48         for (int j = i+1; j <= N; j++) {

49             if (GCD(i, j) == 1) {

50                 fraction.push_back(node(i, j));

51             }

52         }

53     }

54     sort(fraction.begin(), fraction.end(), cmp);

55     for (int i = 0; i < fraction.size(); i++) fout << fraction[i].x << "/" << fraction[i].y << endl;

56 

57     fout << "1/1" << endl;

58 

59     return 0;

60 }

 

你可能感兴趣的:(action)