首先看一下题目

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. 网站的高性能架构---Web前端性能优化

    网站性能测试 不同视角下的网站性能 用户视角的网站性能:从用户角度,网站性能就是用户在浏览器上直观感受到的网站响应速度.用户的感受时间包括用户计算机和网站服务器通信的时间.网站服务器处理请求时间.用户 ...

  2. tomcat流程原理解析

    tomcat的启动是通过Bootstrap类的main方法(tomcat6开始也可以直接通过Catlina的main启动) Bootstrap的启动 Bootstrap的main方法先new了一个自己 ...

  3. Jlink下载问题

    在使用Jlink SWD模式进行下载的时候遇到了无法下载的问题. SWD模式下,共有4跟线,VCC.GND.SWCLK.SWDIO JTAG标准接口如下图所示: 一般情况下,目标板卡的 debug V ...

  4. springboot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  5. Neo4j 第二篇:图形数据库

    在深入学习图形数据库之前,首先理解属性图的基本概念.一个属性图是由顶点(Vertex),边(Edge),标签(Lable),关系类型和属性(Property)组成的有向图.顶点也称作节点(Node), ...

  6. 第 5 章 MySQL 备份与恢复

    第 5 章 MySQL 备份与恢复 前言 数据库的备份与恢复一直都是 DBA 工作中最为重要的部分之一,也是基本工作之一.任何正式环境的数据库都必须有完整的备份计划和恢复测试,本章内容将主要介绍 My ...

  7. 012一对一 唯一外键关联映射_双向(one-to-one)

    ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...

  8. 浏览器兼容之Chrome浏览器: -webkit-text-size-adjust: none;

    今天在看demo的时候css样式里面发现的 -webkit-text-size-adjust: none; 度娘以后,了解这段样式的作用是:解决Chrome浏览器里面,设置小于12px的字体大小问题. ...

  9. CentOS7下配置网络yum源(附带下载地址)

    一.查看外网是否通畅 配置网络yum源(需要保证外网开通,我这里是使用网易163提供开源镜像站) 二.下载repo文件 cd /etc/yum.repos.dwget http://mirrors.1 ...

  10. Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...