Executing... Test 1: TEST OK [0.005 secs, 3564 KB] Test 2: TEST OK [0.005 secs, 3564 KB] Test 3: TEST OK [0.005 secs, 3564 KB] Test 4: TEST OK [0.005 secs, 3564 KB] Test 5: TEST OK [0.005 secs, 3564 KB] Test 6: TEST OK [0.008 secs, 3564 KB] Test 7: TEST OK [0.014 secs, 3564 KB] Test 8: TEST OK [0.041 secs, 3564 KB] Test 9: TEST OK [0.086 secs, 3564 KB] Test 10: TEST OK [0.132 secs, 3564 KB] Test 11: TEST OK [0.300 secs, 3564 KB] All tests OK.
不是什么优秀的算法,直接暴力穷举所有情况然后排序……
/* TASK:frac1 LANG:C++ */ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int n , bt = 0; struct PII { int f, s; }b[7810]; inline bool operator < (PII A, PII B) { return ((double)A.f/(double)A.s) < ((double)B.f/(double)B.s); } inline int gcd(int x, int y) { int tmp; do{ tmp = x % y; x = y; y = tmp; }while(tmp); return x; } int main() { freopen("frac1.in", "r", stdin); freopen("frac1.out", "w", stdout); std::ios::sync_with_stdio(false); cin >> n; for (int i = 2; i <= n; ++ i) for (int j = 1; j < i; ++ j) if (gcd(i, j) == 1) { ++ bt; b[bt].f = j; b[bt].s = i; } sort(b + 1, b + 1 + bt); cout<<"0/1"<<endl; for (int i = 1; i <= bt; ++ i) cout<<b[i].f<<"/"<<b[i].s<<endl; cout<<"1/1"<<endl; return 0; }