Usaco-section 2.1 Ordered Fractions (sort)

Source: Internet
Author: User
Tags greatest common divisor

Description

Enter a natural number N, for a minimal fraction of A/b (numerator and denominator coprime fractions), to meet 1<=b<=n,0<=a/b<=1, find all the scores that meet the criteria.

Here is an example, when n=5, all solutions are:

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

Given a natural number n,1<=n<=160, programming outputs all the solutions in the order in which the fractional values increment.

Note: The greatest common divisor of ①0 and any natural number is the natural number ② coprime refers to the two natural numbers that greatest common divisor equals 1.

format

Program NAME: Frac1

INPUT FORMAT:

(File frac1.in)

Single line one natural number n (1..160)

OUTPUT FORMAT:

(File frac1.out)

Each score is a single row, in order of size

SAMPLE INPUT
5
SAMPLE OUTPUT
0/11/51/41/32/51/23/52/33/44/51/1


The data is small, and all the numbers are stored directly in the array, and then sorted.

Note: Only the simplest fraction is put into the array, because if it is not the simplest fraction, it will be able to be found in the array after simplification


/*id:your_id_hereprog:frac1lang:c++*/#include <cstdio> #include <algorithm>using namespace std;struct Fact {    int a, b;    BOOL operator < (const fact& x) const {        return a*x.b<x.a*b;    }} F[12999];int Main () {    int i,j,n,num;    Freopen ("Frac1.in", "R", stdin);    Freopen ("Frac1.out", "w", stdout);    while (1==SCANF ("%d", &n)) {        printf ("0/1\n");        for (Num=0,i=2;i<=n;++i) for            (j=1;j<i;++j) {                if (__GCD (i,j) ==1) {//did not look closely at the sample, in the Recruit ... If it is not the simplest fraction, it is not counted (the simplest fraction is already in the array)                    f[num].a=j;                    f[num++].b=i;                }            }        Sort (f,f+num);        for (i=0;i<num;++i)            printf ("%d/%d\n", f[i].a,f[i].b);        printf ("1/1\n");    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Usaco-section 2.1 Ordered Fractions (sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.