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种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...
 
随机推荐
- android studio导入矢量svg图标技巧
			
1.在android studio中打开File-->Settings-->Plugins,在Plugins中输入SVG2VectorDrawable搜索插件,并安装. 2.安装完成重启a ...
 - 在LINUX上创建GIT服务器
			
转载 如果使用git的人数较少,可以使用下面的步骤快速部署一个git服务器环境. 1. 生成 SSH 公钥 每个需要使用git服务器的工程师,自己需要生成一个ssh公钥进入自己的~/.ssh目录,看有 ...
 - HDU4738(割边)
			
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
 - 初识Dapper
			
16年年底开发一个项目,拍卖的项目,对于我这个不入流的程序员来说,雪微是个挑战.程序猿这个行业就是学到老用到老吧.个人比较喜欢sql原生的写法,对EF 还是不怎么感冒,EF 虽然强大,但是用起来还不怎 ...
 - atom编辑器快捷键
			
挑来挑去,还是决定选择atom,做为我的编程编辑器. 下面是我总结的atom快捷键 //1.atomcmd+,; 设置cmd+h; 隐藏程序cmd+alt+h; 隐藏其他程序 //2.文件cmd+n; ...
 - 【java设计模式】之 模板方法(Template Method)模式
			
1. 模板方法的一个实例 这一节主要来学习一下设计模式中的模板方法模式.我们先来看一个例子:假如现在老板让你做一个汽车的模型,要求只要完成基本功能即可,不考虑扩展性,那你会怎么做呢?我们首先会根据经验 ...
 - 使用C#读写ini配置文件
			
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...
 - ion-scroll zooming="true" android端无法缩放的问题
			
很久很久没更新博客了,从今天开始决定以后陆续写一些博文,总结下自己在开发中碰到的问题. ionic项目.ion-scroll zooming="true" 在android端无法缩 ...
 - SQL2012还原数据库操作在本地服务器上操作和用别的电脑远程连接到服务器进行操作的文件路径差异
			
在数据库服务器上想还原一个数据库到某个备份文件时期的,服务器的数据库文件本身是保存在 D:\DEVDB目录 通过开发电脑上的MS manager来连接数据库服务器操作还原 虽发现文件卡项上,原始文件名 ...
 - Java多线程程序休眠、暂停与停止
			
休眠 在Java多线程中,可以使用sleep()方法在指定毫秒数内让当前正在执行的线程休眠. 下面这段代码,使得主函数的main线程休眠了2000ms,最后输出的间隔时间也是2000ms. p ...