# EOJ 1852 Solution Report --Ordered Fractions

csdn这排版没法用了，掀桌(╯‵□′)╯︵┻━┻

## 2. 解题思路

### 解法1

• 分母不超过 N
• 分子与分母互质
• 分子小于分母

 1 2 3 4  for(int i = 1; i <= n-1; i++) for(int j = i+1; j <= n; j++) if(gcd(i, j) == 1) // gcd denotes 最大公约数 /* store valid fractions here*/ 

 1 2 3 4  struct fraction { int numerator; int denominator; }; 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70  // // main.cpp // eoj1852_struct // // Created by whyisyoung on 2/27/14. // Copyright (c) 2014 whyisyoung. All rights reserved. // #include #include #include #include using namespace std; struct fraction { int numerator; int denominator; }frac[7820]; int gcd(int a, int b) { return (b == 0) ? a : (gcd(b, a % b)); } int generate_fractions(int n) { int count = 0; for(int i = 1; i <= n-1; i++) { for(int j = i+1; j <= n; j++) { if(gcd(i, j) == 1) { frac[count].numerator = i; frac[count++].denominator = j; } } } return count; } int compare(const void* a, const void* b) { fraction *x = (fraction *)a; fraction *y = (fraction *)b; double frac_a = x->numerator * 1.0 / x->denominator; double frac_b = y->numerator * 1.0 / y->denominator; if(frac_a < frac_b) return -1; if(frac_a > frac_b) return 1; return 0; } int main(int argc, char const *argv[]) { int n, count; while(scanf("%d", &n) != EOF) { count = generate_fractions(n); cout << "0/1\n"; qsort(frac, count, sizeof(frac[0]), compare); for(int i = 0; i < count; i++) cout << frac[i].numerator << '/' << frac[i].denominator << endl; cout << "1/1\n"; } return 0; } 

### 解法2

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  // // main.cpp // eoj1852 // // Created by whyisyoung on 2/23/14. // Copyright (c) 2014 whyisyoung. All rights reserved. // #include #include #include #include #include #include #include using namespace std; map frac; int gcd(int a, int b) { return (b == 0)?a:(gcd(b, a % b)); } int generate_fractions(int n) { int size = 0; for (int i = 1; i <= n-1; ++i) { for(int j = i+1; j <= n; j++) { if(gcd(i, j) == 1) { char cstr[10]; sprintf(cstr, "%d/%d", i, j); // itoa(i, cstr, 10) is deprecated string str(cstr); frac[i*1.0 / j] = str; size++; } } } return size; } int main(int argc, char const *argv[]) { int n, size; while(cin >> n) { size = generate_fractions(n); cout << "0/1\n"; map::iterator it = frac.begin(); for( ; it != frac.end(); it++) { cout << it->second << endl; } cout << "1/1\n"; } return 0; } 

### 解法3

 1 2 3 4 5 6 7 8 9  int N; void Feray(int a, int b, int c, int d) { if(b + d > N) return ; Feray(a, b, a+c, b+d); cout << a+c << '/' << b+d << endl; Feray(a+c, b+d, c, d); } 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  void farey(int n) { int pa=0, pb=1, ca=1, cb=n; int na, nb; printf("%d/%d\n", pa, pb); printf("%d/%d\n", ca, cb); while(ca!=cb){ na = (pb+n) / cb * ca - pa; nb = (pb+n) / cb * cb - pb; printf("%d/%d\n", na, nb); pa = ca; pb = cb; ca = na; cb = nb; } }

• 本文已收录于以下专栏：

举报原因： 您举报文章：EOJ 1852 Solution Report --Ordered Fractions 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)