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. CentOS6.5下编译R源码并安装Spark R

    不多说,直接上干货! 为了使用SparkR,决定要在Spark所在的Linux上装上R,结果血泪篇了.主要原因是公司内部的虚机,无法连外网,所以网上很多的直接rpm或者yum的方法都没用,需要自己编译 ...

  2. JS form 表单收集 数据 formSerialize

    做后台系统的时候通常会用到form表单来做数据采集:每次一个字段一个字段的去收集就会很麻烦,网站也有form.js插件可以进行表单收集,并封装成一个对象,通过ajax方法传到后台:现在介绍一种直觉采集 ...

  3. java连接AD域

    import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.Hashtable; imp ...

  4. 图片放大不失真软件PhotoZoom如何使用?

    PhotoZoom可以将我们一些过于像素低的照片可以无失真放大,那么PhotoZoom是如何实现无失真照片放大的呢? 以上图像中的编号表示每个步骤应操作的位置. 单击“打开”,并选择您想调整大小的图像 ...

  5. .apply .call方法的区别及使用 .apply第二个参数为数组,.call第二个参数为参数列表, 相同点:第一个参数都为Function函数内部的this对象.

    Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...

  6. BZOJ 3456: 城市规划 多项式求逆

    Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了.  刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得整个国家的任意两个城市都直接 ...

  7. reMarkable安装教程

    PS :每次都下一遍安装包挺无奈的...... 系统版本 :Ubuntu 16.04 安装包 :remarkable_1.87_all.deb 链接 Here!-> reMarkable 安装步 ...

  8. centos查看防火墙端口

    #centos7启动防火墙 systemctl start firewalld.service #centos7停止防火墙/关闭防火墙 systemctl stop firewalld.service ...

  9. ZOJ 3888 Twelves Monkeys

    Twelves Monkeys Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  10. hdu 2491 贪心

    #include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int u,v,len,time; }m ...