USACO 2.1 Ordered Fractions
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的更多相关文章
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 洛谷P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...
- 洛谷——P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- 洛谷 P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- 【USACO 2.1】Ordered Fractions
/* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚 ...
- USACO Ordered Fractions
首先看一下题目 Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less t ...
- USACO Section 2.1 Ordered Fractions
/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...
- USACO Section 2.1 Ordered Fractions 解题报告
题目 题目描述 给定一个数N(1<=N<=160),需要产生所有的分数,这些分数的值必须要在0~1之间.而且每个分数的分母不能超过N.如下例所示: N = 5 产生所有的分数:0/1 1/ ...
- [Swust OJ 801]--Ordered Fractions
题目链接:http://acm.swust.edu.cn/problem/801/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
随机推荐
- java中的访问修饰符2
综上所述:protected强调的是子类,deafult强调的是本包,private强调的是本类,public强调的是开放性.
- 3D旋转立方体案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- hdu3938 Portal 离线的并查集
离线算法是将全部输入都读入,计算出所有的答案以后再输出的方法.主要是为避免重复计算.类似于计算斐波那契数列的时候用打表的方法. 题目:给一个无向图,求有多少个点对,使得两点间的路径上的花费小于L,这里 ...
- 关于H5移动端开发 iPhone X适配
一. 媒体查询. @media screen and (device-width:375px) and (device-height:812px){ #header { height: 88px; p ...
- JavaScript Cookies使用
Cookie 是个存储在客户端(浏览器)记录信息确定用户身份的小文本文件,可以用来跟踪用户当前登陆状态和用户浏览页面的次数,记录用户输入的文本信息,也可以在页面间传递变量,记录用户一些行为. 当浏览器 ...
- 『MicroPython』Hello uPy
官网买了几乎全套.一路曲折:7月10号下单,13号发货,14号法兰克福过关,23号到北京,25号到上海,27号到沪C:沪C邮局投3次未果,中彩票一样终于打通了投递部电话才在次日28号“妥投”:又因出差 ...
- ASP组件AspJpeg(加水印)生成缩略图等使用方法
ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出 ...
- Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )
题意: 三角形数.五边形数和六角形数分别由以下公式给出: 三角形数 Tn=n(n+1)/2 1, 3, 6, 10, 15, - 五边形数 Pn=n(3n−1)/2 1, 5, 12, 2 ...
- [NoiPlus2016]天天爱跑步
巨坑 树剖学的好啊!---sfailsth 把一段路径拆成两段,向上和S->LCA,向下LCA->T 用维护重链什么的操作搞一下. sfailsth学长真不容易啊...考场上rush了4. ...
- 3.1、Jinja2模板引擎
形式最简单的 Jinja2 模板就是一个包含响应文本的文件.示例 3-1 是一个 Jinja2 模板,它和示例 2-1 中 index() 视图函数的响应一样. 示例 3-1 templates/in ...