Usaco 2.1 Ordered Fractions

Source: Internet
Author: User
Tags cmath

Ordered Fractions

Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.

Here's 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 this, given an integer N between 1 and inclusive, prints the fractions in order of increasing Magnitud E.

Program Name:frac1input 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/11/51/41/32/51/23/52/33/44/51/1

The main topic: that is, given an n, the output value between 0 and 1, the denominator of 1 to N of all values of the non-repetition of the score (you can numerator need numerator).

Idea: Very simple, because the amount of data is small, so it is to enumerate the numerator denominator, and then not the most simple fraction of the score, and then sort.

1 /*2 ID:FFFGRDC13 Prob:frac14 lang:c++5 */6#include <cstdio>7#include <iostream>8#include <algorithm>9#include <cmath>Ten using namespacestd; One intprime[ -],primecnt=0; A BOOLbo[ $]={0}; - structStr - { the     intXinty; -     Doubleans; -}e[40000]; - BOOLkong (str aa,str BB) + { -     returnaa.ans<Bb.ans; + } A BOOLCheckintXinty) at { -     //int temp=sqrt (double (y)); -      for(intI=1; i<=primecnt&&prime[i]<=y;i++) -     { -         if(! (y%Prime[i])) -             if(! (%Prime[i])) in                 return 0; -     } to     return 1; + } - intMain () the { *Freopen ("frac1.in","R", stdin); $Freopen ("Frac1.out","W", stdout);Panax Notoginseng     intN; -scanf"%d",&n); the     intAnscnt=0; +bo[0]=bo[1]=1; A      for(intI=2;i< $; i++) the     { +         if(!Bo[i]) -         { $prime[++primecnt]=i; $              for(intj=2;j*i< $; j + +) -             { -bo[i*j]=1; the             } -         }Wuyi     } the      for(intI=1; i<=n;i++) -     { Wu          for(intj=1; j<i;j++) -         { About             if(check (j,i)) $             { -e[++anscnt].x=J; -e[anscnt].y=i; -E[anscnt].ans= (j*1.0)/i; A             } +         } the     } -Sort (e+1, e+1+Anscnt,kong); $printf"0/1\n"); the      for(intI=1; i<=anscnt;i++) the     { theprintf"%d/%d\n", e[i].x,e[i].y); the     } -printf"1/1\n"); in     return 0; the}

Check function is too bad to write ... WA made a few rounds because he wanted to optimize it. Originally was thinking with GCD, but worry about the complexity of the problem, later seniors told me not to worry about it, and even without their own handwriting, algorithm inside there are ready ... So the code turns into a, and the complexity drops .... I'm surprised.

1 /*2 ID:FFFGRDC13 Prob:frac14 lang:c++5 */6#include <cstdio>7#include <iostream>8#include <algorithm>9#include <cmath>Ten using namespacestd; One intprime[ -],primecnt=0; A BOOLbo[ $]={0}; - structStr - { the     intXinty; -     Doubleans; -}e[40000]; - BOOLkong (str aa,str BB) + { -     returnaa.ans<Bb.ans; + } A BOOLCheckintXinty) at { -     return__GCD (x, y) = =1; - } - intMain () - { -Freopen ("frac1.in","R", stdin); inFreopen ("Frac1.out","W", stdout); -     intN; toscanf"%d",&n); +     intAnscnt=0; -bo[0]=bo[1]=1; the      for(intI=2;i< $; i++) *     { $         if(!Bo[i])Panax Notoginseng         { -prime[++primecnt]=i; the              for(intj=2;j*i< $; j + +) +             { Abo[i*j]=1; the             } +         } -     } $      for(intI=1; i<=n;i++) $     { -          for(intj=1; j<i;j++) -         { the             if(check (j,i)) -             {Wuyie[++anscnt].x=J; thee[anscnt].y=i; -E[anscnt].ans= (j*1.0)/i; Wu             } -         } About     } $Sort (e+1, e+1+Anscnt,kong); -printf"0/1\n"); -      for(intI=1; i<=anscnt;i++) -     { Aprintf"%d/%d\n", e[i].x,e[i].y); +     } theprintf"1/1\n"); -     return 0; $}

Usaco 2.1 Ordered Fractions

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.