当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Error occurred while trying to proxy request The project suddenly can't get up

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

查询表中开始日期与结束日期
![Valid bracketed strings [greedy exercise]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)
Valid bracketed strings [greedy exercise]

Tensorflow中实现正则化

Weka 3.8.6安装与Weka 3.8.6功能介绍

升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
![[Geek Challenge 2020] Roamphp1-Welcome](/img/3b/2fa91f7478b8abf6efe0feafd24e58.png)
[Geek Challenge 2020] Roamphp1-Welcome

基于模糊PID的液压舵机伺服系统

华为无线设备配置Mesh业务
随机推荐
PyTorch 猫狗分类源代码及数据集
信息学奥赛一本通 1915:【01NOIP普及组】最大公约数与最小公倍数 | 洛谷 P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
592. Fraction Addition and Subtraction
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
主流的深度学习推理架构有哪些呢?
Valid bracketed strings [greedy exercise]
论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》
Microsoft Office 2019 software download and installation detailed tutorial!
什么是工业射线照相设备?
SLIM: Sparse Linear Methods (TopN推荐)
华为无线设备配置Mesh业务
优酷视频元素内容召回系统:多级多模态引擎探索
Insert data into MySQL in C language
论文阅读之《Color Constancy Using CNNs》
理解实现搜索二叉树
FP6606CMP5 CPC-16L USB类型-C和PD充电控制器 百盛电子代理商
Prometheus 基本概念
olap——入门ClickHouse
windwons 下GPU环境和pytorch安装
基于MATLAB的电力系统短路故障分析与仿真