• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            EOJ 1852. Ordered Fractions

            Ordered Fractions

            Time Limit:1000MSMemory Limit:30000KB
            Total Submit:191Accepted:122

            Description

            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.

            Input

            One line with a single integer N.

            Output

            One fraction per line, sorted in order of magnitude.

            Sample Input

            5

            Sample Output

            0/1
            1/5
            1/4
            1/3
            2/5
            1/2
            3/5
            2/3
            3/4
            4/5
            1/1

            Source

            usaco




            解法一:
            生成所有分數,排序,去重。
            我剛開始就是這么干的。

            解法二:
            老肖教的。
            Farey 數列。
            本題實際上是要求輸出 Farey 數列。


            遞歸生成:

             1/*
             2EOJ 1852 Ordered Fractions 
             3
             4Farey 數列。
             5
             6*/

             7
             8
             9#include <stdio.h>
            10
            11int n;
            12
            13void farey( int a1, int b1, int a2, int b2 ) {
            14        if ( b1 + b2 > n ) {
            15                return;
            16        }

            17        farey( a1, b1, a1+a2, b1+b2 );
            18        printf( "%d/%d\n", a1+a2, b1+b2 );
            19        farey( a1+a2, b1+b2, a2, b2 );
            20}

            21
            22int main() {
            23        scanf( "%d"&n );
            24        puts( "0/1" );
            25        farey( 0111 );
            26        puts( "1/1" );
            27        return 0;
            28}

            29


            迭代生成:

             1/*
             2EOJ 1852 Ordered Fractions 
             3
             4Farey 數列。
             5
             6*/

             7
             8
             9#include <stdio.h>
            10
            11void farey( int n ) {
            12        int preA, preB, curA, curB, nxtA, nxtB;
            13
            14        preA = 0;
            15        preB = 1;
            16        curA = 1;
            17        curB = n;
            18
            19        printf( "%d/%d\n", preA, preB );
            20        printf( "%d/%d\n", curA, curB );
            21        while ( curA != curB ) {
            22                nxtA = ( preB + n ) / curB * curA - preA;
            23                nxtB = ( preB + n ) / curB * curB - preB;
            24                printf( "%d/%d\n", nxtA, nxtB );
            25                preA = curA;
            26                preB = curB;
            27                curA = nxtA;
            28                curB = nxtB;
            29        }

            30}

            31
            32int main() {
            33        int n;
            34        scanf( "%d"&n );
            35        farey( n );
            36        return 0;
            37}

            38

            posted on 2012-02-29 19:33 coreBugZJ 閱讀(407) 評論(0)  編輯 收藏 引用 所屬分類: ACM 、Algorithm 、Mathematics課內作業

            亚洲精品美女久久久久99| 久久久久亚洲AV成人网人人网站 | 久久精品亚洲日本波多野结衣| 色婷婷综合久久久久中文| 99久久久精品免费观看国产| 久久e热在这里只有国产中文精品99 | 四虎国产精品免费久久| 日韩精品久久久久久久电影蜜臀| 国产韩国精品一区二区三区久久| 亚洲国产成人精品91久久久| 久久综合综合久久狠狠狠97色88 | 久久精品麻豆日日躁夜夜躁| 少妇被又大又粗又爽毛片久久黑人| 久久av无码专区亚洲av桃花岛| 亚洲国产成人乱码精品女人久久久不卡| AV无码久久久久不卡蜜桃| 久久久黄色大片| 久久国产精品无码网站| 久久被窝电影亚洲爽爽爽| 久久99国产精品尤物| 伊人久久大香线蕉亚洲五月天| 四虎影视久久久免费观看| 日韩一区二区久久久久久 | 久久久精品国产免大香伊| 久久亚洲2019中文字幕| 九九99精品久久久久久| 久久精品无码一区二区无码| 亚洲AV无一区二区三区久久| 亚洲伊人久久大香线蕉综合图片| 久久亚洲国产精品五月天婷| 成人国内精品久久久久影院VR | 亚洲欧美国产日韩综合久久| 久久精品国产第一区二区| 久久一区二区三区免费| 欧美激情精品久久久久久久| 亚洲国产小视频精品久久久三级| 精品久久久久久久久免费影院| 中文字幕无码久久人妻| 综合久久精品色| 亚洲精品美女久久777777| 久久久久久亚洲AV无码专区|