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 (超级源点)
随机推荐
- 安装服务Memcached+Nginx+Php linux下安装
Memcached安装 1. 源码安装libevent(下载地址:http://monkey.org/~provos/libevent/) 2. 源码安装memcached(下载地 ...
- 【Mysql】初学命令行指南
MYSQL初学者使用指南与介绍 一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbi ...
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
目前建立在TCP协议上的网络协议特别多,有telnet,ssh,有ftp,有http等等.这些协议又可以根据数据吞吐量来大致分成两大类:(1)交互数据类型,例如telnet,ssh,这种类型的协议在大 ...
- [转] TreeList 当前节点图标和背景色设置
高原之上原文TreeList 选中节点时图标状态和背景色 // 给TreeList加SelectImage this.treelArea.SelectImageList = imglCustom; / ...
- XTUOJ 1252 Defense Tower 贪心
题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252 思路:考虑每条边对玩家的伤害 假设连接的节点是u,v,破坏 ...
- UITableView 点击展开的实现
推介看下这里的 内容 http://www.cnblogs.com/kenshincui/p/3931948.html IOS8 above UITabliViewCell 利用 autolayou ...
- 常见设计模式解析和实现(C++)Prototype模式(原型模式)
作用:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. UML结构图: 抽象基类: 1) Prototype:虚拟基类,所有原型的基类,提供Clone接口函数 接口函数: 1) P ...
- (一)使用Blender导出GameMaker支持的模型脚本
源于YOYO论坛帖子:http://gmc.yoyogames.com/index.php?showtopic=603723 既然想做3D,那就先从模型的导入开始,具体的源文件,可以在“(二)使用等高 ...
- openfl关于windows平台编译报错解决办法
报错信息: 无法打开程序数据库“e:\newproj\mainclient\bin\windows\cpp\obj\obj\msvc-debug-ncxp\vc.pdb”:如果要将多个 CL.EXE ...
- leetcode—3sum
1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...