标签:
直接双重循环判断最大公约数,如果为1 就保存。
/*
ID: awsd1231
PROG: frac1
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct T {
int a[3];
double v;
// T() {a[1] = ‘/‘;}
}ans[13000];
int n;
int gcd(int a, int b) {
if (a < b) {
int t = a; a = b; b = t;
}
if (b) return gcd(b, a % b);
return a;
}
bool operator < (T a, T b) {
return a.v < b.v;
}
int main () {
freopen("frac1.in", "r", stdin);
freopen("frac1.out", "w", stdout);
scanf("%d", &n);
ans[0].v = 1;
ans[0].a[0] = 1;
ans[0].a[2] = 1;
ans[1].v = 0;
ans[1].a[0] = 0;
ans[1].a[2] = 1;
int idx = 2;
for (double i = 1; i != n + 1; ++i) {
for (double j = 1; j != i; ++j) {
if(!i % 2 && !j % 2) continue;
if(gcd(j, i) == 1) {
ans[idx].v = j/i;
ans[idx].a[0] = j;
ans[idx++].a[2] = i;
}
}
}
sort(ans, ans + idx);
for(int i = 0; i != idx; ++i) {
printf("%d/%d\n", ans[i].a[0], ans[i].a[2]);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/liangyongrui/p/4545465.html