当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
从零开始的Multi-armed Bandit
Metaverse Web 3.0 和 DeFi大师班
un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
FP6600QSO SOP-8 USB专用充电端口控制器 用于快充电协议和QC2.0/3.0
SQLServer下载与安装
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
升级 MDK 5.37 后的问题处理: AC6编译选项, printf, 重启失效等
知识蒸馏1:基础原理讲解及yolov5项目实战介绍
随机推荐
关于内和调试无法查看ntdll内存的问题
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
KDD 2020 | 深入浅出优势特征蒸馏在淘宝推荐中的应用
Promise entry to proficient (1.5w word detailed explanation)
知识蒸馏4:准备数据集并修改网络配置
C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
腾讯专家献上技术干货,带你一览腾讯广告召回系统的演进
JMeter笔记3 | JMeter安装和环境说明
Deep Feedback Network for Recommendation
从零开始的Multi-armed Bandit
bert-base调试心得
记者卧底
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
matlab simulink锂离子电池智能充电策略研究
真正懂经营管理的CIO具备哪些特质
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》
数据库系统原理与应用教程(069)—— MySQL 练习题:操作题 95-100(十三):分组查询与聚合函数的使用
阿里SIM-基于检索的用户行为兴趣CTR模型(Search-based user Interest Model(SIM))