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 (超级源点)
随机推荐
- Android手动画柱状图的例子
效果图如上,网上看到的例子,谨以此文记录一下,以后用到的地方再来翻翻. 核心技术是用Canvas和Paint画长方形. 源码地址:http://download.csdn.net/detail/abc ...
- 【转】使用 Auto Layout 的典型痛点和技巧
layoutIfNeeded()强制立刻更新布局 原文网址:http://www.jianshu.com/p/0f031606e5f2 官方文档:Auto Layout Guide 加上去年WWDC上 ...
- 滑动菜单栏开源项目SlidingMenu的使用
一.SlidingMenu简介相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界面效果,能方便的进行各种操作.很多优秀的应用都采用了这 ...
- C++11笔记
__func__宏 __func__返回当前的函数名,也可以返回class和struct名. /*返回函数名hello*/ const char* hello() { return __func__; ...
- strcasecmp在VS2010中提示未定义标识符
分析: strcasecmp(*,*)是用来比较字符串,定义在string.h头文件中,但是在windows下即使添加string.h头文件,依然会报错. 解决: 添加 #if defined(_MS ...
- 修改linux主机名称
linux修改主机名如下操作即可: [root@kel ~]# cat /etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=no HOSTNAM ...
- bfs CCF2016第七次 游戏
// bfs CCF2016第七次 游戏 // 思路: // O(300*100*100) // 直接暴搜 // 注意,同一格同一时间不能经过两次!!! #include <bits/stdc+ ...
- 多校1005 HDU5785 Interesting (manacher)
// 多校1005 HDU5785 Interesting // 题意:给你一个串,求相邻两个回文串左边端点*右边端点的和 // 思路:马拉车算出最长回文半径,求一个前缀和,既得到每个点对答案的贡献. ...
- GridView控件 Reapter控件 DataList控件 的区别和用法
ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...
- 在已创建的DataTable对象中添加在首列一列
问题描述: 从数据库读取出来的表数据赋给到了DataTable上,将DataTable中数据显示到DataGridView中时希望在DataGridView的第一列显示一列. 解决方法: DataTa ...