• 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之间的所有值不重复的分数(可以约分的需要约分)。

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

     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[100],primecnt=0;
    12 bool bo[200]={0};
    13 struct str
    14 {
    15     int x;int y;
    16     double ans;
    17 }e[40000];
    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=1;i<=primecnt&&prime[i]<=y;i++)
    26     {
    27         if(!(y%prime[i]))
    28             if(!(x%prime[i]))
    29                 return 0;
    30     }
    31     return 1;
    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=0;
    40     bo[0]=bo[1]=1;
    41     for(int i=2;i<200;i++)
    42     {
    43         if(!bo[i])
    44         {
    45             prime[++primecnt]=i;
    46             for(int j=2;j*i<200;j++)
    47             {
    48                 bo[i*j]=1;
    49             }
    50         }
    51     }
    52     for(int i=1;i<=n;i++)
    53     {
    54         for(int j=1;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+1,e+1+anscnt,kong);
    65     printf("0/1
    ");
    66     for(int i=1;i<=anscnt;i++)
    67     {
    68         printf("%d/%d
    ",e[i].x,e[i].y);
    69     }
    70     printf("1/1
    ");
    71     return 0;
    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[100],primecnt=0;
    12 bool bo[200]={0};
    13 struct str
    14 {
    15     int x;int y;
    16     double ans;
    17 }e[40000];
    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)==1;
    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=0;
    33     bo[0]=bo[1]=1;
    34     for(int i=2;i<200;i++)
    35     {
    36         if(!bo[i])
    37         {
    38             prime[++primecnt]=i;
    39             for(int j=2;j*i<200;j++)
    40             {
    41                 bo[i*j]=1;
    42             }
    43         }
    44     }
    45     for(int i=1;i<=n;i++)
    46     {
    47         for(int j=1;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+1,e+1+anscnt,kong);
    58     printf("0/1
    ");
    59     for(int i=1;i<=anscnt;i++)
    60     {
    61         printf("%d/%d
    ",e[i].x,e[i].y);
    62     }
    63     printf("1/1
    ");
    64     return 0;
    65 }
  • 相关阅读:
    Struts2的%,#,$的区别,UI标签及其表单radio,checkbox,select回显数据(七)
    Struts2的控制标签库和数据标签库(六)
    Struts2从后端向前端传递数据和OGNL访问用户自定义静态方法(五)
    两个小例子登录和显示全部用户信息的模块(四)
    Struts2的ServletAPI的获取和各种类型的数据获取(三)
    Action的三种实现方式,struts.xml配置的详细解释及其简单执行过程(二)
    Struts2的 两个蝴蝶飞,你好 (一)
    虚拟机安装Centos7系统后优化操作
    Java进程故障排查(CPU资源占用高,接口响应超时,功能接口停滞等)
    zabbix企业微信报警实现
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/5005682.html
Copyright © 2020-2023  润新知