zoukankan      html  css  js  c++  java
  • USACO Section 2.1 Ordered Fractions

    模拟,开始时以为要注意去重,当输出时,判断与前一个是否重复,后来发现没这必要,如果重复了,那后边那组肯定不是互质的

    analysis里给出了“super fast solution"的方法,类似杨辉三角,想起了高中时的阿炳老师--两个肩上数之和~~

     1 /* ID:linyvxi1
    2 PROB:frac1
    3 LANG:C++
    4 */
    5 #include <stdio.h>
    6 #include <algorithm>
    7 using namespace std;
    8 typedef struct Frac{
    9 int up;
    10 int down;
    11 }Frac;
    12 Frac frac[100000];
    13 int total_solutions=1;
    14 bool cmp(Frac a,Frac b)
    15 {
    16 return ((float)a.up/a.down)<((float)b.up/b.down);
    17 }
    18 int gcd(int a,int b)
    19 {
    20 int temp;
    21 while(b){
    22 temp=a%b;
    23 a=b;
    24 b=temp;
    25 }
    26 return a;
    27 }
    28 void trans(int n)
    29 {
    30 int i;
    31 for(i=1;i<n;i++){
    32 if(gcd(n,i)==1){
    33 frac[total_solutions].up=i;
    34 frac[total_solutions++].down=n;
    35 }
    36 }
    37 }
    38
    39 int main()
    40 {
    41 int n;
    42 freopen("frac1.in","r",stdin);
    43 freopen("frac1.out","w",stdout);
    44 scanf("%d",&n);
    45 int i=1;
    46 for(i=2;i<=n;i++){
    47 trans(i);
    48 }
    49 sort(frac+1,frac+total_solutions,cmp);
    50 printf("0/1\n");
    51 for(i=1;i<total_solutions;i++){
    52 printf("%d",frac[i].up);
    53 putchar('/');
    54 printf("%d\n",frac[i].down);
    55 }
    56 printf("1/1\n");
    57 }



  • 相关阅读:
    算法(两数之和)
    算法(动态规划二)
    算法(动态规划一)n个学生问题
    Navigator 对象
    Window 对象
    JavaScript 运算符
    JavaScript 全局属性/函数
    JavaScript RegExp 对象
    JavaScript String 对象
    JavaScript Number 对象
  • 原文地址:https://www.cnblogs.com/yangce/p/2354562.html
Copyright © 2011-2022 走看看