[Swust OJ 8010--Ordered Fractions

题目链接:http://acm.swust.edu.cn/problem/801/

Time limit(ms): 1000      Memory limit(kb): 10000

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

题目大意:给定一个数,代表分母的最大值,从小到大,输出所有分母小于N,值小于等于1的所有分数

直接上代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #define maxn 1000010
 5 using namespace std;
 6 struct node{
 7     int x, y;
 8     bool operator<(const node &tmp)const{
 9         if (x != tmp.x)
10             return x*tmp.y < y*tmp.x;
11         return y > tmp.y;
12     }
13 }a[maxn];
14 int gcd(int a, int b){
15     return !b ? a : gcd(b, a%b);
16 }
17 int main(){
18     int n, i, j;
19     while (~scanf("%d", &n)){
20         int pos = 0;
21         //i代表分母,j代表分子
22         for (i = 1; i <= n; i++){
23             for (j = 0; j <= i; j++){
24                 if (!j&&i != 1) continue;
25                 if (gcd(j, i) == 1){
26                     a[pos].x = j;
27                     a[pos++].y = i;//  x/y
28                 }
29             }
30         }
31         sort(a, a + pos);
32         for (i = 0; i < pos; i++)
33             printf("%d/%d\n", a[i].x, a[i].y);
34     }
35     return 0;
36 }

时间: 2024-11-03 08:04:39

[Swust OJ 8010--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

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is

洛谷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&

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

[Swust OJ 404]--最小代价树(动态规划)

题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ (2+3))=((5)+(5))=10.除去原数不4,1,2,3之外,其余都为中间结果,如5,5,10,将中间结果相加

洛谷 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和任意自然数的最大公约数就是那个自然数②互质指最大公约数等于