USACO 2.1.2 Ordered Fractions

来源:互联网 发布:超薄笔记本推荐 知乎 编辑:程序博客网 时间:2024/04/29 22:20

一.题目描述

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

二.题目分析

由于题目是出现在第二章节的填充算法中的,一开始我尝试向BFS,DFS方向想,事实是并没有用。。。简单来想,只是一个枚举加互质判断的过程,由于最后需要有序输出,当然要用到qsort了。智商有限,只能想到这种肤浅的做法,官方给出了很好的答案,再次也给出,大家可以脑补一下哈。

三.代码

1.民间版

#include <stdio.h>#include <stdlib.h>#define MAX 13000typedef struct fra{    int a,b;   //a代表分子,b代表分母}Frac;int gcd(int x,int y){    if(x==0)        return y;    if(y==0)        return x;    if(x<y)        return gcd(y,x);    return gcd(y,x%y);}int cmp(const void*a,const void*b){    Frac x1=*(Frac*)a;    Frac x2=*(Frac*)b;    int t1=x1.b,t2=x2.b;   //一定要先把原分母存起来,否则下面会乘乱的,第一次就是这么出错的。    x1.b=x1.b*t2;    x1.a=x1.a*t2;    x2.b=x2.b*t1;    x2.a=x2.a*t1;    return x1.a-x2.a;}int main(){    int N,i,j,k;    Frac f[MAX];    FILE *in=fopen("frac1.in","r"),*out=fopen("frac1.out","w");    if(!in||!out)    {        printf("file open error!\n");        return -1;    }    fscanf(in,"%d",&N);    k=0;    for(i=2;i<=N;i++)    {        for(j=1;j<i;j++)        {            if(gcd(i,j)==1)            {                f[k].a=j;                f[k].b=i;                k++;            }        }    }    qsort(f,k,sizeof(f[0]),cmp);    fprintf(out,"0/1\n");    for(i=0;i<k;i++)        fprintf(out,"%d/%d\n",f[i].a,f[i].b);    fprintf(out,"1/1\n");    return 0;}

2.官方

Here's a super fast solution from Russ: We notice that we can start with 0/1 and 1/1 as our ``endpoints'' and recursively generate the middle points by adding numerators and denominators. 0/1                                                              1/1                               1/2                  1/3                      2/3        1/4              2/5         3/5                 3/4    1/5      2/7     3/8    3/7   4/7   5/8       5/7         4/5Each fraction is created from the one up to its right and the one up to its left. This idea lends itself easily to a recursion that we cut off when we go too deep. #include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>int n;FILE *fout;/* print the fractions of denominator <= n between n1/d1 and n2/d2 */voidgenfrac(int n1, int d1, int n2, int d2){if(d1+d2 > n)/* cut off recursion */return;genfrac(n1,d1, n1+n2,d1+d2);fprintf(fout, "%d/%d\n", n1+n2, d1+d2);genfrac(n1+n2,d1+d2, n2,d2);}voidmain(void){FILE *fin;fin = fopen("frac1.in", "r");fout = fopen("frac1.out", "w");assert(fin != NULL && fout != NULL);fscanf(fin, "%d", &n);fprintf(fout, "0/1\n");genfrac(0,1, 1,1);fprintf(fout, "1/1\n");}



0 0
原创粉丝点击