当前位置:网站首页>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;
}
边栏推荐
- 快使用flyway管理sql脚本吧~
- 论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
- 腾讯专家献上技术干货,带你一览腾讯广告召回系统的演进
- 952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题
- Servo System of Hydraulic Steering Gear Based on Fuzzy PID
- 莫队--优雅的暴力
- Py程序员的七夕情人节
- 强烈推荐APP破解常用工具集合!
- 宝塔搭建PHP自适应懒人网址导航源码实测
- 一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
猜你喜欢
随机推荐
超声波探伤仪是做什么用的?
Moralis去中心化Web3应用开发教程
图注意力机制
esp32系列(5):esp32 蓝牙架构学习
优酷视频元素内容召回系统:多级多模态引擎探索
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
什么是无损检测设备?
fast shell porting
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
Microsoft Office 2019 software download and installation detailed tutorial!
LeetCode318: Maximum product of word lengths
torch.optim.Adam() function usage
知识蒸馏4:准备数据集并修改网络配置
论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
大批程序员可能面临被劝退!
华为无线设备配置Mesh业务
记者卧底
【云商店公告】关于7月30日帮助中心更新通知
链表Oj练习题 纯C语言
阿里巴巴CAN:Embedding前置的特征交互新思路









