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:

  1. 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)

  1. 5

OUTPUT FORMAT

One fraction per line, sorted in order of magnitude.

SAMPLE OUTPUT (file frac1.out)

  1. 0/1
  2. 1/5
  3. 1/4
  4. 1/3
  5. 2/5
  6. 1/2
  7. 3/5
  8. 2/3
  9. 3/4
  10. 4/5
  11. 1/1

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

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

  1. /*
  2. ID:fffgrdc1
  3. PROB:frac1
  4. LANG:C++
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<algorithm>
  9. #include<cmath>
  10. using namespace std;
  11. int prime[],primecnt=;
  12. bool bo[]={};
  13. struct str
  14. {
  15. int x;int y;
  16. double ans;
  17. }e[];
  18. bool kong(str aa,str bb)
  19. {
  20. return aa.ans<bb.ans;
  21. }
  22. bool check(int x,int y)
  23. {
  24. //int temp=sqrt(double (y));
  25. for(int i=;i<=primecnt&&prime[i]<=y;i++)
  26. {
  27. if(!(y%prime[i]))
  28. if(!(x%prime[i]))
  29. return ;
  30. }
  31. return ;
  32. }
  33. int main()
  34. {
  35. freopen("frac1.in","r",stdin);
  36. freopen("frac1.out","w",stdout);
  37. int n;
  38. scanf("%d",&n);
  39. int anscnt=;
  40. bo[]=bo[]=;
  41. for(int i=;i<;i++)
  42. {
  43. if(!bo[i])
  44. {
  45. prime[++primecnt]=i;
  46. for(int j=;j*i<;j++)
  47. {
  48. bo[i*j]=;
  49. }
  50. }
  51. }
  52. for(int i=;i<=n;i++)
  53. {
  54. for(int j=;j<i;j++)
  55. {
  56. if(check(j,i))
  57. {
  58. e[++anscnt].x=j;
  59. e[anscnt].y=i;
  60. e[anscnt].ans=(j*1.0)/i;
  61. }
  62. }
  63. }
  64. sort(e+,e++anscnt,kong);
  65. printf("0/1\n");
  66. for(int i=;i<=anscnt;i++)
  67. {
  68. printf("%d/%d\n",e[i].x,e[i].y);
  69. }
  70. printf("1/1\n");
  71. return ;
  72. }

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

  1. /*
  2. ID:fffgrdc1
  3. PROB:frac1
  4. LANG:C++
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<algorithm>
  9. #include<cmath>
  10. using namespace std;
  11. int prime[],primecnt=;
  12. bool bo[]={};
  13. struct str
  14. {
  15. int x;int y;
  16. double ans;
  17. }e[];
  18. bool kong(str aa,str bb)
  19. {
  20. return aa.ans<bb.ans;
  21. }
  22. bool check(int x,int y)
  23. {
  24. return __gcd(x,y)==;
  25. }
  26. int main()
  27. {
  28. freopen("frac1.in","r",stdin);
  29. freopen("frac1.out","w",stdout);
  30. int n;
  31. scanf("%d",&n);
  32. int anscnt=;
  33. bo[]=bo[]=;
  34. for(int i=;i<;i++)
  35. {
  36. if(!bo[i])
  37. {
  38. prime[++primecnt]=i;
  39. for(int j=;j*i<;j++)
  40. {
  41. bo[i*j]=;
  42. }
  43. }
  44. }
  45. for(int i=;i<=n;i++)
  46. {
  47. for(int j=;j<i;j++)
  48. {
  49. if(check(j,i))
  50. {
  51. e[++anscnt].x=j;
  52. e[anscnt].y=i;
  53. e[anscnt].ans=(j*1.0)/i;
  54. }
  55. }
  56. }
  57. sort(e+,e++anscnt,kong);
  58. printf("0/1\n");
  59. for(int i=;i<=anscnt;i++)
  60. {
  61. printf("%d/%d\n",e[i].x,e[i].y);
  62. }
  63. printf("1/1\n");
  64. return ;
  65. }

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. java中的访问修饰符2

    综上所述:protected强调的是子类,deafult强调的是本包,private强调的是本类,public强调的是开放性.

  2. 3D旋转立方体案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. hdu3938 Portal 离线的并查集

    离线算法是将全部输入都读入,计算出所有的答案以后再输出的方法.主要是为避免重复计算.类似于计算斐波那契数列的时候用打表的方法. 题目:给一个无向图,求有多少个点对,使得两点间的路径上的花费小于L,这里 ...

  4. 关于H5移动端开发 iPhone X适配

    一. 媒体查询. @media screen and (device-width:375px) and (device-height:812px){ #header { height: 88px; p ...

  5. JavaScript Cookies使用

    Cookie 是个存储在客户端(浏览器)记录信息确定用户身份的小文本文件,可以用来跟踪用户当前登陆状态和用户浏览页面的次数,记录用户输入的文本信息,也可以在页面间传递变量,记录用户一些行为. 当浏览器 ...

  6. 『MicroPython』Hello uPy

    官网买了几乎全套.一路曲折:7月10号下单,13号发货,14号法兰克福过关,23号到北京,25号到上海,27号到沪C:沪C邮局投3次未果,中彩票一样终于打通了投递部电话才在次日28号“妥投”:又因出差 ...

  7. ASP组件AspJpeg(加水印)生成缩略图等使用方法

    ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出 ...

  8. 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 ...

  9. [NoiPlus2016]天天爱跑步

    巨坑 树剖学的好啊!---sfailsth 把一段路径拆成两段,向上和S->LCA,向下LCA->T 用维护重链什么的操作搞一下. sfailsth学长真不容易啊...考场上rush了4. ...

  10. 3.1、Jinja2模板引擎

    形式最简单的 Jinja2 模板就是一个包含响应文本的文件.示例 3-1 是一个 Jinja2 模板,它和示例 2-1 中 index() 视图函数的响应一样. 示例 3-1 templates/in ...