Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分
B. Nearest Fraction
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/281/problem/B
Description
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction whose denominator is no more than n.
Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value is as minimal as possible.
If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator.
Input
A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 105).
Output
Print the required fraction in the format "a/b" (without quotes).
Sample Input
3 7 6
Sample Output
2/5
HINT
题意
给你a,b,n,让你找一个分数出来,使得其分母不大于n,并且这个分数最接近a/b的最简分数
题解:
直接暴力枚举分母,然后再二分分子就好了
然后更新答案
代码
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std; int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int main()
{
int a,b,n;
cin>>a>>b>>n;
if(n>=b)
{
printf("%d/%d\n",a/gcd(a,b),b/gcd(a,b));
return ;
}
double x = a*1.0 / b*1.0;
int ans1 = ,ans2 = ;
for(int i=;i<=n;i++)
{
int l = ,r = ;
while(l<=r)
{
int mid = (l+r)/;
if(mid*1.0/(i*1.0)>=x)r=mid-;
else l=mid+;
}
if(fabs((l-)*1.0/(i*1.0)-x)<fabs((ans1*1.0)/(ans2*1.0)-x))
ans1 = l-,ans2 = i;
if(fabs((l)*1.0/(i*1.0)-x)<fabs((ans1*1.0)/(ans2*1.0)-x))
ans1 = l,ans2 = i;
}
printf("%d/%d\n",ans1/gcd(ans1,ans2),ans2/gcd(ans1,ans2));
}
Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分的更多相关文章
- Codeforces Round #172 (Div. 2)
A. Word Capitalization 模拟. B. Nearest Fraction 枚举. C. Rectangle Puzzle 求出两个矩形的点,套简单多边形的面积交板子. D. Max ...
- Codeforces Round #172 (Div. 1 + Div. 2)
A. Word Capitalization 模拟. B. Nearest Fraction 枚举. C. Rectangle Puzzle 求出两个矩形的点,套简单多边形的面积交板子. D. Max ...
- Codeforces Round #172 (Div. 2) C. Rectangle Puzzle 数学题几何
C. Rectangle Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)
题目链接:http://www.codeforces.com/problemset/problem/281/A题意:将一个英文字母的首字母变成大写,然后输出.C++代码: #include <c ...
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- 【Codeforces Round #433 (Div. 2) A】Fraction
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举分子从高到低就好. 这样得到的一定是最大的. (可以约分没错,但是约分过后和就不是n了,所以不会有错的) [错的次数] 0 [反思] 在这 ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity (超级源点)
随机推荐
- VS2010开发2dx无法解析的外部符号解决记录
首先新建HelloWorld项目... 想使用Cocos2d扩展包需要引入相关头文件,如:#include “cocos-ext.h”...接下来我们右键工程属性->配置属性->c/c++ ...
- ecshop init.php文件分析
1. ecshop init.php文件分析 2. <?php 3. 4. /** 5. * ECSHOP 前台公用文件 6. * ===================== ...
- C# 好用的三层架构,项目直接上手用
一.项目结构 注意:1.Common类中的引用添加:右键--添加引用--.NET--选择对应的引用 2.各层之间引用互相添加(这个就不必多说了,三层最基础部分) 3.在添加 Oracle 引用时候 ...
- bjfu1100 圆环
这题也是2011百度之星的一道题.知道做法后代码极简单. 不过我做完后随便上网搜了一下,发现竟然还有很多不同的做法.别的做法我就不管了,我只把我的做法的原理说清楚.我做题时是按如下顺序逐步找到规律的: ...
- 基本输入输出系统BIOS---显示输出
显示器通过显示适配卡与系统相连, 显示适配卡是显示输出的接口卡,照相的显示器是CGA和EGA,目前的显示适配卡是VGA和TVGA,他们都支持两种显示方式,文本显示和图形显示 在BIOS中提供的显示I/ ...
- JavaScript中的*top、*left、*width、*Height详解
来源:http://www.ido321.com/911.html html代码 1: <body> 2: <div class="father" id=&quo ...
- [Hive - LanguageManual] Describe
Describe Describe Database Describe Table/View/Column Display Column Statistics Describe Partition D ...
- python编码问题的理解与解决
错误:'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) 看到网上很多都不清楚,做了一天的 ...
- 《Java数据结构与算法》笔记-CH2无序数组
/** * 本章目标: * 1.自制数组类 * 2.有序数组:按关键字升降序排列:二分法查找 * 3.分析有序数组.大O表示法 */ /** * 自制数组类 书中有的地方有错误,本程序以修改 */ c ...
- DedeCMS Error:Tag disabled:"php"的解决办法