1684: [Usaco2005 Oct]Close Encounter
1684: [Usaco2005 Oct]Close Encounter
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 387 Solved: 181
[Submit][Status][Discuss]
Description
Lacking even a fifth grade education, the cows are having trouble with a fraction problem from their textbook. Please help them. The problem is simple: Given a properly reduced fraction (i.e., the greatest common divisor of the numerator and denominator is 1, so the fraction cannot be further reduced) find the smallest properly reduced fraction with numerator and denominator in the range 1..32,767 that is closest (but not equal) to the given fraction. 找一个分数它最接近给出一个分数. 你要找的分数的值的范围在1..32767
Input
* Line 1: Two positive space-separated integers N and D (1 <= N < D <= 32,767), respectively the numerator and denominator of the given fraction
Output
* Line 1: Two space-separated integers, respectively the numerator and denominator of the smallest, closest fraction different from the input fraction.
Sample Input
Sample Output
OUTPUT DETAILS:
21845/32767 = .666676839503.... ~ 0.666666.... = 2/3.
HINT
Source
题解:感觉很像是NOIP2014普及组的那道题,貌似当时干掉了好多人= =(HansBug:呵呵哒我会说我第一想法是二分答案么,但是显然二分是没有办法控制分母的大小的)
于是继续脑洞,于是我想到既然分母范围那么小,那么为何不枚举分母呢?然后直接根据原来分数的大致值来估测分子,然后不断打擂台
(PS:值得注意的是要特判和原分数相同的情况,否则你会输入什么就输出什么的= =,不过敢直接这么枚举还是需要一定脑洞哒)
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,l,m,n,a,b:longint;
ans:double;
procedure check(x,y:longint);
var t:double;
begin
if (x*m)=(y*n) then exit;
t:=abs((x/y)-(n/m));
if t<ans then
begin
ans:=t;
a:=x;
b:=y;
end;
end;
begin
readln(n,m);ans:=maxint;;
for j:= to do
begin
i:=trunc((n/m)*j);
check(i,j);
check(i+,j);
end;
writeln(a,' ',b);
readln;
end.
1684: [Usaco2005 Oct]Close Encounter的更多相关文章
- BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MB Description Lacking e ...
- 【BZOJ】1684: [Usaco2005 Oct]Close Encounter(暴力+c++)
http://www.lydsy.com/JudgeOnline/problem.php?id=1684 这货完全在考精度啊.. 比如奇葩 (llf)a/b*i (llf)(a/b*i)和(llf)( ...
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- bzoj1684 [Usaco2005 Oct]Close Encounter
Description Lacking even a fifth grade education, the cows are having trouble with a fraction proble ...
- bzoj1745[Usaco2005 oct]Flying Right 飞行航班*
bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...
- bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)
之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西. 这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备 ...
- bzoj:1685 [Usaco2005 Oct]Allowance 津贴
Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
- BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...
随机推荐
- multipathd dead but pid file exists
构建RAC环境时出现的错误 百度半天未找到解决方案,Google了一下,终于找到可行方案 Solution:- yum update device-mapper glibc -y [root@HE2 ...
- redhat6 + 11G RAC 双节点部署
一.配置网络环境 node1 [root@node1 ~]#vi/etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=no HOSTNAME= ...
- 利用forwardInvocation实现消息重定向
在obj-c中我们可以向一个实例发送消息,相当于c/c++ java中的方法调用,只不过在这儿是说发送消息,实例收到消息后会进行一些处理.比如我们想调用一个方法,便向这个实例发送一个消息,实例收到消息 ...
- 【开发必备】今天我们来谈谈Android NDK动态链接库(so文件)的一些见解
一.写在前面 直到现在,基本我写的每一个项目都会用到NDK动态链接库的知识,可见这个也的确十分常用.那么,今天,咱们就来谈谈它. 二.什么是ABI和so 1.发展 早起的Android系统几乎只支持A ...
- css 清除浮动的方法
/*方法一*/ /*局部清除*/ ;visibility:hidden;display:block;clear:both;} .clr{display:inline-block;} .clr{disp ...
- 如何让Docker容器随宿主机的启动而自动启动
使用Docker容器部署服务时,不仅需要让服务随Docker容器的启动而启动,还需要让Docker容器随宿主机的启动而自动启动,为此Docker提供了Docker run的restart参数 #doc ...
- [html5] 学习笔记-Canvas标签的使用
Canvas通过JavaScript来绘制2D图形.Canvas是逐像素渲染的.在Canvas中,一旦图形被绘制完成,它就不会继续得到浏览器的关注.如果其位置发生变化,那么整个场景也需要重新绘制,包括 ...
- esri-leaflet入门教程(2)-地图的HelloWorld
esri-leaflet入门教程(2)-地图的HelloWorld by 李远祥 常言道"君子性非异也,善假于物也".这句话在IT界同样也适用,只不过IT界有更为时髦的说法:&qu ...
- 如何一秒钟从头构建一个 ASP.NET Core 中间件
前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...
- java初级开发程序员(初识java)
据我们老师讲说:“学习java高级语言,每个程序员最初写的一个程序就是helloword,向世界打招呼! public class HelloWord{ //必须和文件名相同 public sta ...