[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 ...
随机推荐
- 面试题之 query转为obj
要注意处理编码后的字串 对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...
- C# 基础中有关术语理解
一.栈vs堆 深入理解堆栈.堆在内存中的实现 二.Socket 深入探析c# Socket 三.多线程 c# 多线程 --Mutex(互斥锁)
- Vue.js——webpack
Vue.js——60分钟webpack项目模板快速入门 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开 ...
- java中三种常见内存溢出错误的处理方法
更多 10 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...
- JVM内存最大能调多大分析
上次用weblogic 把 -XmxXXXX 设成2G,就启动不起来,设小点就起来了,当时很气,怎么2G都起不了,今天在看到了一篇解释,转过来了这 次一位老友提出了这个问题,记得当年一个java高手在 ...
- appledoc:Objective-C注释文档生成工具
appledoc是帮助Objective-C开发者从特殊格式的源代码注释中生成类似apple资源代码帮助文档的命令行工具. 安装和使用都非常简单: 安装 git clone git://github. ...
- Java面试题之七
三十四.编码转换,怎样实现将GB2312 编码的字符串转换为ISO-8859-1 编码的字符串. String a=new String("中".getBytes("gb ...
- Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法
安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...
- App引导页面源代码的实现
一.页面效果图
- unicode编码相互转换加密解密
需求:把字符串转换成unicode编码加密. 也可以把unicode编码解密并分析出汉字字母数字字符各多少个. unicode编码 \u 后面是一个16进制编码,必要时需要进行转换. 看源码: 0 & ...