Ordered Fractions

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

题目大意:就是说给定一个N,输出值在0到1之间的,分母在1到N之间的所有值不重复的分数(可以约分的需要约分)。

思路:很简单,因为数据量小,所以就是枚举分子分母,然后不要不是最简分数的分数,再排序。

 /*
ID:fffgrdc1
PROB:frac1
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int prime[],primecnt=;
bool bo[]={};
struct str
{
int x;int y;
double ans;
}e[];
bool kong(str aa,str bb)
{
return aa.ans<bb.ans;
}
bool check(int x,int y)
{
//int temp=sqrt(double (y));
for(int i=;i<=primecnt&&prime[i]<=y;i++)
{
if(!(y%prime[i]))
if(!(x%prime[i]))
return ;
}
return ;
}
int main()
{
freopen("frac1.in","r",stdin);
freopen("frac1.out","w",stdout);
int n;
scanf("%d",&n);
int anscnt=;
bo[]=bo[]=;
for(int i=;i<;i++)
{
if(!bo[i])
{
prime[++primecnt]=i;
for(int j=;j*i<;j++)
{
bo[i*j]=;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
if(check(j,i))
{
e[++anscnt].x=j;
e[anscnt].y=i;
e[anscnt].ans=(j*1.0)/i;
}
}
}
sort(e+,e++anscnt,kong);
printf("0/1\n");
for(int i=;i<=anscnt;i++)
{
printf("%d/%d\n",e[i].x,e[i].y);
}
printf("1/1\n");
return ;
}

check函数写的太烂了。。。WA了几发都是因为想优化它。本来是想到用GCD的,但是担心时间复杂度的问题,后来学长告诉我不用担心呀,而且甚至不用自己手写,algorithm里面有现成的。。。于是代码变成下面这样也A了,而且复杂度下降了。。。。惊了

 /*
ID:fffgrdc1
PROB:frac1
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int prime[],primecnt=;
bool bo[]={};
struct str
{
int x;int y;
double ans;
}e[];
bool kong(str aa,str bb)
{
return aa.ans<bb.ans;
}
bool check(int x,int y)
{
return __gcd(x,y)==;
}
int main()
{
freopen("frac1.in","r",stdin);
freopen("frac1.out","w",stdout);
int n;
scanf("%d",&n);
int anscnt=;
bo[]=bo[]=;
for(int i=;i<;i++)
{
if(!bo[i])
{
prime[++primecnt]=i;
for(int j=;j*i<;j++)
{
bo[i*j]=;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
if(check(j,i))
{
e[++anscnt].x=j;
e[anscnt].y=i;
e[anscnt].ans=(j*1.0)/i;
}
}
}
sort(e+,e++anscnt,kong);
printf("0/1\n");
for(int i=;i<=anscnt;i++)
{
printf("%d/%d\n",e[i].x,e[i].y);
}
printf("1/1\n");
return ;
}

USACO 2.1 Ordered Fractions的更多相关文章

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

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

  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 Ordered Fractions

    首先看一下题目 Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less t ...

  7. USACO Section 2.1 Ordered Fractions

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

  8. USACO Section 2.1 Ordered Fractions 解题报告

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

  9. [Swust OJ 801]--Ordered Fractions

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

随机推荐

  1. Devexpress PdfViewer预览pdf,禁止下载,打印,复制

    PDFviewer控件: 参数设置: 1.屏蔽书签栏和右键菜单 2.加载文档支持路径以及流stream加载的方式 pdfViewer.MenuManager.DisposeManager(); pdf ...

  2. 以SqlHelper为例论面向对象中封装的使用(续)

    上文以SqlHelper为例说明了面向对象中封装的好处,但是上文只是简单封装,考虑下面代码的情况: public static Activate GetByCode(string code) { Li ...

  3. javascript中五句话

    1.弹出框 ,小括号中就是弹出的内容 alert("我是一个弹出框");   2.控制台输出 小括号里面就是 控制台输出的东西 console.log("我是控制台输出的 ...

  4. Bin文件

    那什么是bin文件呢?为什么这么关键? bin (binary)既是:二进制, 里面存放的一般是可执行的二进制文件.二进制即是机器代码,汇编语言编译后的结果.我们编译的是高级语言,把高级语言翻译为机器 ...

  5. [翻译]内存一致性模型 --- memory consistency model

    I will just give the analogy with which I understand memory consistency models (or memory models, fo ...

  6. SciSharpCube:容器中的SciSharp,.NET机器学习开箱即用

    SciSharp Cube 在Docker容器中快速体验SciSharp机器学习工具的最新功能. 项目地址:https://github.com/SciSharp/SciSharpCube 从Dock ...

  7. 转载:移动端自适应:flexible.js可伸缩布局使用

    阿里团队开源的一个库.flexible.js,主要是实现在各种不同的移动端界面实现一稿搞定所有的设备兼容自适应问题. 实现方法: 通过JS来调整html的字体大小,而在页面中的制作稿则统一使用rem这 ...

  8. Redis详解入门篇(转载)

    Redis详解入门篇(转载) [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 ...

  9. java 常用API 包装 数据转换

    package com.oracel.demo01; public class Sjzh { // 将基本数据类型转字符串 public static void main(String[] args) ...

  10. Git 常用命令速查(转载)

    git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r 查看远程所有分支git co ...