首先看一下题目

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. JAVA中线程的状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW:至今尚未启动的线程的状态. RUNNABLE:可运行线程的线程状态. ...

  2. SpringMVC中的@Controller和@RequestMapping到底什么鬼?

    1.1 @Controller是什么 首先看个例子: @Controller @RequestMapping("/blog") public class BlogControlle ...

  3. angular.js封装的文件上传指令

    今天把最近用到的东西整理一下,直接上代码,需要申请犀牛存储图片,文件 1.html div div img.img-thumbnail.center-block(ng-src="{{ltUp ...

  4. TP框架 命名空间 与第三方类

    命名空间 相当于虚拟目录 所有类文件都放在虚拟目录 功能:实现自动加载类 TP框架的命名空间要更复杂 内容=> 命名空间中定义和使用 都用\1初始命名空间 相当于 根目录 如:Library文件 ...

  5. SpringBoot系列(一)RestTemplate

    作为springBoot的开篇系列,RestTemplate只能表示我只是个意外 what RestTemplate是spring提供的用于访问rest服务的客户端(其实类似Apache的HttpCl ...

  6. swift学习 - tableView自适应高度1(xib autoLayout)

    tableView自适应高度 效果图: 源码: class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSo ...

  7. JBoss7安装、测试、配置和启动以及停止,部署

    转:http://www.hongyanliren.com/2014m01/3013.html 内容概要 JBoss系列三主要目的是演示如何部署应用到JBoss7/WildFly,如下图中描述了部署应 ...

  8. 调整分区大小(ext2\ext3\ext4)

    现在的时间是2017年5月27日 我想说调整分区大小没那么麻烦,至少我直接将一个ext3格式分区从50G减少到了30G. 步骤如下 1 首先确保那个分区是没被挂载的. 2 调整分区大小,但是其实并没有 ...

  9. HTML中部分标签的嵌套问题

    书写HTML结构的时候,对于标签的嵌套问题,在我发现这个问题之前,都不在自己的考虑之中,还傻傻的以为标签之间是可以进行百搭的! 其实,有些标签是不能进行随意嵌套,如果你没有深受其害,你是不会发现它的存 ...

  10. GPU编程--宏观理解篇(1)

    GPU编程与CPU编程最大的不同可以概括为以下两点: "The same program is executed on many data elements in parallel" ...