题目链接:http://acm.swust.edu.cn/problem/801/

Time limit(ms): 1000      Memory limit(kb): 10000
 
Description

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.

 
Sample Input
5
 
Sample Output
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
 
 
题目大意:给定一个数,代表分母的最大值,从小到大,输出所有分母小于N,值小于等于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的更多相关文章

  1. 洛谷P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...

  2. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  3. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  4. 洛谷——P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...

  5. USACO 2.1 Ordered Fractions

    Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominat ...

  6. 洛谷 P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...

  7. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  8. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  9. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

随机推荐

  1. sql server 修改表自增列的值

    Create PROCEDURE [dbo].[SP_UpdateIdentityId] ( ) , @beforeId INT , @afterId INT ) AS BEGIN IF @befor ...

  2. 老生常谈--Js继承小结

    一直以来,对Js的继承有所认识,但是认识不全面,没什么深刻印象.于是,经常性的浪费很多时间重新看博文学习继承,今天工作不是特别忙,有幸看到了http://www.slideshare.net/stoy ...

  3. HDU 5820 Lights(扫描线+zkw线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5820 [题目大意] 在一个大小为50000*50000的矩形中,有n个路灯. 询问是否每一对路灯之 ...

  4. android之PackageManager简单介绍

    PackageManager相关 本类API是对全部基于载入信息的数据结构的封装,包含下面功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...

  5. angularJS使用$watch监控数据模型的变化

    使用$watch监控数据模型的变化 在scope 内置的全部函数中,用得最多的可能就是$watch 函数了.当你的数据模型中某一部分发生变化时,$watch 函数能够向你发出通知.你能够监控单个对象的 ...

  6. 新浪微博开放平台开发-android客户端(1)【转】

    http://www.cnblogs.com/virusswb/archive/2011/08/05/2128941.html 最近不是太忙,花了一些时间学习android的应用开发.经过两个星期的学 ...

  7. jQeury学习笔记

    jQuery 语法: 核心语法: $(selector).action() 美元符号定义 jQuery 选择符(selector)"查询"和"查找" HTML ...

  8. Java学习之抽象类的总结

    抽象类的特点:1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰,抽象方法必须定义在抽象类中,该类必须也被abstract修饰.2,抽象类不可以被实例化.为什么?因为调用抽象方 ...

  9. parquet code demo

    http://www.programcreek.com/java-api-examples/index.php?source_dir=hiped2-master/src/main/java/hip/c ...

  10. React使用笔记(3)-React Event Listener

    Date: 2015-11-28 12:18 Category: Web Tags: JavaScript Author: 刘理想 [toc] 1. 构造基本结构 首先,我们先创建一个按钮,一个输入框 ...