USACO2.1.2 Ordered Fractions (frac1)

来源:互联网 发布:湖南电大网络形考平台 编辑:程序博客网 时间:2024/05/21 06:42
因为n最大只有160所以枚举。

枚举每一个分子和分母,如果互质则记录,最后排序。

/*ID:shijiey1PROG:frac1LANG:C++*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Node {int a, b;bool operator < (const Node &n) const {double c = (double)a / (double)b;double d = (double)n.a / (double)n.b;return c < d;}}arr[20000];int cnt = 0;int n;int gcd(int a, int b) {if (a < b) swap(a, b);if (!b && a == 1) return 1;if (!a || !b) return 0;if (!(a % b)) return b;return gcd(b, a % b);}int main() {freopen("frac1.in", "r", stdin);freopen("frac1.out", "w", stdout);scanf("%d", &n);for (int i = 1; i <= n; i++) {for (int j = 0; j < i; j++) {if (gcd(i, j) == 1) {Node t;t.a = j;t.b = i;arr[cnt++] = t;}}}sort(arr, arr + cnt);for (int i = 0; i < cnt; i++) {printf("%d/%d\n", arr[i].a, arr[i].b);}printf("1/1\n");return 0;}


0 0