USACO Ordered Fractions
来源:互联网 发布:windows系统模拟器 编辑:程序博客网 时间:2024/06/07 09:47
首先看一下题目
Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.
Here is the set when N = 5:
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.
PROGRAM NAME: frac1
INPUT FORMAT
One line with a single integer N.
SAMPLE INPUT (file frac1.in)
5
OUTPUT FORMAT
One fraction per line, sorted in order of magnitude.
SAMPLE OUTPUT (file frac1.out)
0/11/51/41/32/51/23/52/33/44/51/1由于数据量很小的缘故,我们可以把所有的分数存下来,然后进行排序。第一步,存下所有的已约分的分数。第二步,对存下来的分数进行排序。至此,此题完成。
/**ID: njuwz151TASK: frac1LANG: C++*/#include <bits/stdc++.h>using namespace std;const int maxn = 165;int n;typedef struct { int x; int y;} Frac;Frac frac[maxn*maxn];int cmp(Frac a, Frac b);int gcd(int a, int b);int main() { freopen("frac1.in", "r", stdin); freopen("frac1.out", "w", stdout); cin >> n; int count = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j <= i; j++) {// cout << i << " " << j << " " << gcd(i, j) << endl; if(gcd(i, j) == 1) { frac[count].x = j; frac[count].y = i; count++; } } } sort(frac, frac + count, cmp); for(int i = 0; i < count; i++) { cout << frac[i].x << "/" << frac[i].y << endl; }}int cmp(Frac a, Frac b) { return a.x * b.y < b.x * a.y;}int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b);}
阅读全文
0 0
- 【其他】【USACO】Ordered Fractions
- USACO Ordered Fractions
- [usaco] 2.1 Ordered Fractions
- usaco Ordered Fractions
- USACO-Ordered Fractions
- USACO Ordered Fractions
- USACO 2.1 Ordered Fractions
- USACO 2.1 Ordered Fractions
- USACO--2.1Ordered Fractions
- USACO 2.1 Ordered Fractions
- USACO Ordered Fractions(枚举)
- USACO 2.1 Ordered Fractions
- USACO [2.1] Ordered Fractions
- USACO 2.1 Ordered Fractions
- USACO Ordered Fractions
- usaco training Ordered Fractions
- Usaco Section 2.1 Ordered Fractions
- USACO :Ordered Fractions解题报告
- 遇到的Mysql的一个坑
- 【Android】Android Studio常用快捷键
- mysql 单服务器创建多实例
- USACO The Castle
- github本地git push ssh方式免用户名和密码配置相关问题
- USACO Ordered Fractions
- vue组件中数据的传递
- USACO Sorting a Three-Valued Sequence
- U8SDK打包出现Duplicate file错误导致打包失败的解决方法
- html中canvas遇到的问题
- iOS-如何消除警告
- USACO Healthy Holsteins
- SASS学习
- 利用 Hadoop FileSystem listStatus 遍历文件目录 实现HDFS操作