当前位置:网站首页>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;
}
边栏推荐
- 顺通海关查验预约综合管理系统
- C# 连接SQL Sever 数据库与数据查询实例 数据仓库
- 莫队--优雅的暴力
- Express framework connects MySQL and ORM framework
- Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
- 592. Fraction Addition and Subtraction
- 测试.net文字转语音模块System.Speech
- Promise entry to proficient (1.5w word detailed explanation)
- C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
- 数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系
猜你喜欢

Metaverse Web 3.0 和 DeFi大师班

18.支持向量机(SVM)的介绍

一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版

Py程序员的七夕情人节

Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法

LeetCode167: Sum of two numbers in sorted array

Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
![Valid bracketed strings [greedy exercise]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)
Valid bracketed strings [greedy exercise]

Excel导入和导出

基于MATLAB的电力系统短路故障分析与仿真
随机推荐
C# 连接SQL Sever 数据库与数据查询实例 数据仓库
信息学奥赛一本通 1966:【14NOIP普及组】比例简化 | 洛谷 P2118 [NOIP2014 普及组] 比例简化
C陷阱与缺陷 第6章 预处理器 6.1 不能忽视宏定义中的空格
FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
js中的基础知识点 —— BOM
Microsoft Office 2019 软件下载安装详细教程!
强烈推荐APP破解常用工具集合!
Wanhua chemical fine chemical industry innovation product assembly
什么是无损检测设备?
主流的深度学习推理架构有哪些呢?
基于stm32的shell实现
un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
测试行业干了5年,从只会点点点到了现在的测试开发,总算是证明了自己
Summary of String Copy, Concatenation, Comparison and Split Functions (1)
Promise entry to proficient (1.5w word detailed explanation)
莫队--优雅的暴力
Graph Attention Mechanism
真正懂经营管理的CIO具备哪些特质
将 APACHE 日志解析到 SQL 数据库中