当前位置:网站首页>Informatics Olympiad All-in-One 1966: [14NOIP Popularization Group] Scale Simplification | Luogu P2118 [NOIP2014 Popularization Group] Scale Simplification
Informatics Olympiad All-in-One 1966: [14NOIP Popularization Group] Scale Simplification | Luogu P2118 [NOIP2014 Popularization Group] Scale Simplification
2022-07-30 17:41:00 【Your righteousness _noip】
【题目链接】
ybt 1966:【14NOIP普及组】比例简化
洛谷 P2118 [NOIP2014 普及组] 比例简化
【题目考点】
1. 枚举
【解题思路】
观察L的范围,为1~100,So it can be enumerated separatelyA’与B’的值,determine which groupA’/B’ ≥ A/B,并找出A’/B’- A/Bthe smallest set of values.
对于A’与B’是否互质,It can be determined in advance whether the greatest common divisor of the two is 1.
You can actually not do this step.由于A’与B’They are all traversed from small to large,对于A’/B’the same ratio,Must be traversed firstA’与B’互质的情况,Then traverse to the case where the two are not mutually prime.Only need to use when finding the minimum value’<',The former case can be taken without the latter case.
【题解代码】
解法1:Use the greatest common divisor function to determine whether the two are relatively prime
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int gcd(int a, int b)
{
if(b == 0)
return a;
return gcd(b, a%b);
}
int main()
{
int a, b, l, ri, rj;//ri,rj记录结果
double d, d1, mind = INF;//mind:最小差值
cin >> a >> b >> l;
d = (double)a/b;//A/B
for(int i = 1; i <= l; ++i)
for(int j = 1; j <= l; ++j)
{
//i:A' j:B'
if(gcd(i, j) == 1)//最大公约数为1,二者互质
{
d1 = (double)i/j;
if(d1 >= d && d1-d < mind)
{
mind = d1-d;
ri = i;
rj = j;
}
}
}
cout << ri << ' ' << rj;
return 0;
}
解法2:Do not judge mutual quality
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int a, b, l, ri, rj;//ri,rj记录结果
double d, d1, mind = INF;//mind:最小差值
cin >> a >> b >> l;
d = (double)a/b;//A/B
for(int i = 1; i <= l; ++i)
for(int j = 1; j <= l; ++j)
{
//i:A' j:B'
d1 = (double)i/j;
if(d1 >= d && d1-d < mind)
{
mind = d1-d;
ri = i;
rj = j;
}
}
cout << ri << ' ' << rj;
return 0;
}
边栏推荐
- Prometheus 基本概念
- 升级Win11后不喜欢怎么退回Win10系统?
- 分账系统二清解决方案如何助力电商平台合规经营?
- 数据库系统原理与应用教程(066)—— MySQL 练习题:操作题 71-81(十):连接查询
- FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
- 阿里巴巴CAN:Embedding前置的特征交互新思路
- 大批程序员可能面临被劝退!
- Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
- Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
- 想要写出好的测试用例,先要学会测试设计
猜你喜欢
随机推荐
论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》
C语言向MySQL插入数据
esp32系列(5):esp32 蓝牙架构学习
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
S7-200SMART中定时器的使用方法和常见注意事项汇总
592. Fraction Addition and Subtraction
顺通海关查验预约综合管理系统
今年这情况。。真心推荐专科的工程师升个本!
想要写出好的测试用例,先要学会测试设计
bert-base调试心得
[HarekazeCTF2019] Avatar Uploader 1
olap——入门ClickHouse
知识蒸馏1:基础原理讲解及yolov5项目实战介绍
matlab simulink锂离子电池智能充电策略研究
数据库系统原理与应用教程(067)—— MySQL 练习题:操作题 82-89(十一):数据的增、删、改操作
Promise entry to proficient (1.5w word detailed explanation)
升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
Graph Attention Mechanism
web服务通过用户访问请求判断设备来源



![[MRCTF2020]Ezaudit](/img/80/d4656abdff20703591ffdc3f5a5ebc.png)




