首先看一下题目

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/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1 由于数据量很小的缘故,我们可以把所有的分数存下来,然后进行排序。
第一步,存下所有的已约分的分数。
第二步,对存下来的分数进行排序。
至此,此题完成。
/**
ID: njuwz151
TASK: frac1
LANG: C++
*/
#include <bits/stdc++.h> using namespace std; const int maxn = ; 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 = ;
for(int i = ; i <= n; i++) {
for(int j = ; j <= i; j++) {
// cout << i << " " << j << " " << gcd(i, j) << endl;
if(gcd(i, j) == ) {
frac[count].x = j;
frac[count].y = i;
count++;
}
}
} sort(frac, frac + count, cmp);
for(int i = ; 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 == ? a : gcd(b, a % b);
}

USACO Ordered Fractions的更多相关文章

  1. USACO 2.1 Ordered Fractions

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

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

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

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

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

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

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

  5. 【USACO 2.1】Ordered Fractions

    /* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚 ...

  6. USACO Section 2.1 Ordered Fractions

    /* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...

  7. USACO Section 2.1 Ordered Fractions 解题报告

    题目 题目描述 给定一个数N(1<=N<=160),需要产生所有的分数,这些分数的值必须要在0~1之间.而且每个分数的分母不能超过N.如下例所示: N = 5 产生所有的分数:0/1 1/ ...

  8. USACO Section2.1 Ordered Fractions 解题报告

    frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  9. [Swust OJ 801]--Ordered Fractions

    题目链接:http://acm.swust.edu.cn/problem/801/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

随机推荐

  1. OGNL表达式与EL表达式

    一.OGNL表达式 a)什么是OGNL? OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言, 通过它简单一致的表达式语法.主要功能:    ...

  2. JAVA虚拟机系列文章

    本系列文章主要记录自己在学习<深入理解Java虚拟机-JVM高级特性与最佳实践>的知识点总结,文章内容都是基于周志明所著书籍的总结. 1.Java内存区域与溢出 2.垃圾收集器与内存分配策 ...

  3. java之 Timer 类的使用以及深入理解

    最近一直在看线程知识,然后看到Timer定时器使用了线程实现的定时功能,于是了解了解: 本文 从Time类的使用和源码分析两个方面讲解: 1---Timer类使用 2---源码分析 1.Time类使用 ...

  4. UITextField关闭自动联想功能

    在textField输入内容时,如果内容为英文,输入的英文如果不正确的单词就是有红色的线报警,关闭英文自动联想功能 self.autocorrectionType = UITextAutocorrec ...

  5. php的八大数据类型

    1. 八大数据: bool 布尔类型:0,1:真假 integer 整形 float 浮点型 string 字符串 array 数组 object 对象,类,class resource 文件,图片, ...

  6. [原创]LAMP+phpmyadmin+FTP环境搭建

    ***简单ftp服务器搭建: rpm –qa|grep vsftpd   //检查是否安装服务 yum –y install vsftpd-*   //安装服务 mkdir /var/ftp/uplo ...

  7. 忘记block格式 xib加载没有计算导航栏和tabbar的大小

    敲inlineBlock xib加载没有计算导航栏和tabbar的大小 /将这个属性改为no self.tabBarController.tabBar.translucent = NO; 判断优化,两 ...

  8. arcgis sde 导出栅格文件失败,提示“Database user name and current user schema do not match ”.

    具体错误/警告如下: 翻译一下:数据库用户名和当前用户数据库对象的集合不匹配 没有空间参考存在 数据库表没找到 主要还是第一句的问题. 解决方法:切换当前sde账户为能够写入sde的账户,这块不是很了 ...

  9. Spring Boot的properties配置文件读取

    我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...

  10. keyup实现在输入状态不发送搜索请求,停止输入后发送

    个人需求:通过keyup事件配合后台elasticsearch(弹性搜索),用户在输入状态不发送请求,等停止输入后发送请求. 这是个思考笔记,因为项目临时需要弹性搜索功能,所以临时想了这么个法子,方法 ...