Ordered Fractions--最大公约数+pair
2016-03-13 10:38
477 查看
题意:给最简分数排序
分析:pair的基本使用,用pair来存一下分数,把分子分母最大公约数为1的加入数组,然后排序一下就好了。
Code:
分析:pair的基本使用,用pair来存一下分数,把分子分母最大公约数为1的加入数组,然后排序一下就好了。
Code:
<span style="font-family:Microsoft YaHei;font-size:18px;">#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-6; const int maxn = 160+5; pair<int, int> p[maxn*maxn]; int n; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.first*1.0/p1.second - p2.first*1.0/p2.second < (-eps); } int main(){ while(scanf("%d", &n) != EOF) { int cnt = 0; for(int i = 1; i <= n; i++) for(int j = 0;j <= i; j++) if(gcd(i, j) == 1) p[cnt++] = make_pair(j, i); sort(p, p+cnt, cmp); for(int i = 0; i < cnt; i++) printf("%d/%d\n", p[i].first, p[i].second); } return 0; } </span>
相关文章推荐
- 【leetcode】Array——Contains Duplicate II(219)
- 使用 flask-mail 扩展发送邮件
- sakai的工程
- pthread_cond_wait()条件变量
- [leetcode] 256. Paint House 解题报告
- AI和IA之随想
- 阿尔法完胜,人类怎么办?
- OC高效率52不要使用retainCount
- JetBrains WebStorm for Mac 10.0.4 破解版 – 下载地址及安装方法
- onSavaInstanceState的用法
- AI设计彭罗斯悖论三角教程
- 多线程 : 使用 wait 和 notify 实现进程间同步通信
- C++ 自定义namespace的使用技巧 http://jingyan.baidu.com/article/b7001fe19399fa0e7282ddb3.html
- 【leetcode】Array——Contains Duplicate(217)
- urlretrieve() 函数http://www.nowamagic.net/academy/detail/1302861
- AI感官模拟
- bzoj 3540: [Usaco2014 Open]Fair Photography
- adb failed to start daemon 彻底解决
- Aizu 0525 Osenbei【枚举+dfs】
- 人工智能——电脑模拟人类学习