EOJ(排序)——1852. Ordered Fractions

该博客介绍了EOJ的一道编程题,要求编写程序对分子和分母小于或等于N的分数集合进行排序,输出值升序排列。博主解析了题目,提出使用结构体存储分数,并通过二重循环和最大公约数判断来排除不可约分数。解决方案包括分数的通分比较策略。
摘要由CSDN通过智能技术生成
1852. 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 1 and 160 inclusive, prints the fractions in order of increasing magnitude.

输入

One line with a single integer N.

输出

One fraction per line, sorted in order of magnitude.
样例

input

5

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的不保存。排序时可以将两分数通分,便于比较大小。

具体代码:
#include<iostream>
#include<algorithm>
using namespace std;

struct node{
   
	int a;//分子 
	int b
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值