2.1.2 Ordered Fractions

直接双重循环判断最大公约数,如果为1 就保存。

用一个结构体保存值和对应数据。重载下< 即可,比较简单。

  1. /*
  2. ID: awsd1231
  3. PROG: frac1
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<algorithm>
  9. using namespace std;
  10. struct T {
  11. int a[3];
  12. double v;
  13. // T() {a[1] = ‘/‘;}
  14. }ans[13000];
  15. int n;
  16. int gcd(int a, int b) {
  17. if (a < b) {
  18. int t = a; a = b; b = t;
  19. }
  20. if (b) return gcd(b, a % b);
  21. return a;
  22. }
  23. bool operator < (T a, T b) {
  24. return a.v < b.v;
  25. }
  26. int main () {
  27. freopen("frac1.in", "r", stdin);
  28. freopen("frac1.out", "w", stdout);
  29. scanf("%d", &n);
  30. ans[0].v = 1;
  31. ans[0].a[0] = 1;
  32. ans[0].a[2] = 1;
  33. ans[1].v = 0;
  34. ans[1].a[0] = 0;
  35. ans[1].a[2] = 1;
  36. int idx = 2;
  37. for (double i = 1; i != n + 1; ++i) {
  38. for (double j = 1; j != i; ++j) {
  39. if(!i % 2 && !j % 2) continue;
  40. if(gcd(j, i) == 1) {
  41. ans[idx].v = j/i;
  42. ans[idx].a[0] = j;
  43. ans[idx++].a[2] = i;
  44. }
  45. }
  46. }
  47. sort(ans, ans + idx);
  48. for(int i = 0; i != idx; ++i) {
  49. printf("%d/%d\n", ans[i].a[0], ans[i].a[2]);
  50. }
  51. return 0;
  52. }

来自为知笔记(Wiz)

时间: 2024-08-29 10:02:19

2.1.2 Ordered Fractions的相关文章

USACO 2.1 Ordered Fractions

Ordered Fractions 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

洛谷P1458 顺序的分数 Ordered Fractions

P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解为: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 给定一个自然数N,1<=n&

洛谷 P1458 顺序的分数 Ordered Fractions

P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解为: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 给定一个自然数N,1<=n<=160,请编程按分数值递增的顺序输出所有解. 注:①0和任意自然数的最大公约数就是那个自然数②互质指最大公约数等于

USACO Section 2.1 Ordered Fractions

/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; typedef struct Fraction { int numerator; int denominator; Fraction(){} Fraction(int x, int

Project Euler:Problem 71 Ordered fractions

Consider the fraction, n/d, where n and d are positive integers. If n<d and HCF(n,d)=1, it is called a reduced proper fraction. If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get: 1/8, 1/7, 1/6, 1/5, 1/4, 2/7,

欧拉工程第71题:Ordered fractions

题目链接:https://projecteuler.net/problem=71 If n<d and HCF(n,d)=1, it is called a reduced proper fraction. n/d 真分数升序排序后,离 3/7最近的数,d<=1000000 Java程序: public class P71{ void run(){ calculate(); } void calculate(){ int max_n = 1000000; long a = 3; long b

USACO Ordered Fractions

题目大意:求一个n的farey序列 思路:懒得麻烦的推导和公式了,暴力压倒一切 /*{ ID:a4298442 PROB:frac1 LANG:C++ } */ #include<cstdio> #include<iostream> #include<algorithm> #include<fstream> #define maxn 160*160+10 using namespace std; ifstream fin("frac1.in&quo

Ordered Fractions

链接 分析:遍历一下,求个gcd即可,最后按照ans排序并去重 1 /* 2 PROB:frac1 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #include "algorithm" 11 #include "v

USACO Section2.1 Ordered Fractions 解题报告

frac1解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N,对于所有的既约分数i/j(1<=j<=N,0<=i<=j),升序排列输出(重复则只留j最小的).[数据范围]

【USACO 2.1】Ordered Fractions

/* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚举,约分,排序,去重 */ #include<cstdio> #include<algorithm> using namespace std; struct node{ int nu,deno; double v; }a[40000]; int n,cnt; int cmp(node