[Swust OJ 801]--Ordered Fractions
题目链接:http://acm.swust.edu.cn/problem/801/
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.
Input
One line with a single integer N.
Output
One fraction per line, sorted in order of magnitude.
| 5 |
|
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
|
#include <iostream>
#include <algorithm>
#include <cstdio>
#define maxn 1000010
using namespace std;
struct node{
int x, y;
bool operator<(const node &tmp)const{
if (x != tmp.x)
return x*tmp.y < y*tmp.x;
return y > tmp.y;
}
}a[maxn];
int gcd(int a, int b){
return !b ? a : gcd(b, a%b);
}
int main(){
int n, i, j;
while (~scanf("%d", &n)){
int pos = ;
//i代表分母,j代表分子
for (i = ; i <= n; i++){
for (j = ; j <= i; j++){
if (!j&&i != ) continue;
if (gcd(j, i) == ){
a[pos].x = j;
a[pos++].y = i;// x/y
}
}
}
sort(a, a + pos);
for (i = ; i < pos; i++)
printf("%d/%d\n", a[i].x, a[i].y);
}
return ;
}
[Swust OJ 801]--Ordered Fractions的更多相关文章
- 洛谷P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- 洛谷——P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- USACO 2.1 Ordered Fractions
Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominat ...
- 洛谷 P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
随机推荐
- linux 怎么查找oracle11g的安装目录
一般来说,/etc/oraInst.loc文件里会记录oracle的路径,如[oracle@ruby ~]$ cat /etc/oraInst.loc inventory_loc=/u01/app/o ...
- Groovy中那些神奇注解之InheritConstructors
上一篇:Groovy中那些神奇注解之ToString 写完ToString,本来想今天就写到这了,突然觉得InheritConstructors注解实在也是个神器,写起来也没多少字,还是写了吧. In ...
- android HTTP发送及MD5加密收集
发送部分: public void MyFunction{ HttpClient httpclient = new DefaultHttpClient(); //你的URL HttpPost http ...
- bootstrap 更改container 的width
参考:http://stackoverflow.com/questions/15884102/bootstrap-how-do-i-change-the-width-of-the-container ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- POJ 2758 Checking the Text(Hash+二分答案)
[题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...
- swift学习第五章-字典的使用
//以下是关于字典的 //字典的格式[key:value] //字典能够存放基本类型和对象类型的 //声明一个字典 var dictionary1=["key1":"鸭鸭 ...
- SQL Server索引进阶:第一级,索引简介
这个并不是我翻译的,全文共有15篇,但我发现好多网站已经不全,所以自己整理. 原文地址: Stairway to SQL Server Indexes: Level 1, Introduction t ...
- 初步认识ExtJS
最近因为项目,需要去学习ExtJS的相关内容. 现在对于ExtJS完全是小白一枚. 目前使用的是ExtJS4.2的版本,官网上现在最新版本是6的. 第一个方法:Ext.onReady() Ext.on ...
- SQL Server 2012学习笔记 1 命令行安装
setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=748RB-X4T6B-MRM7V-RTVFF-CHC8H /FEATU ...