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

2 3

Sample Output

21845 32767

OUTPUT DETAILS:

21845/32767 = .666676839503.... ~ 0.666666.... = 2/3.

HINT

 

Source

Silver

题解:感觉很像是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的更多相关文章

  1. BZOJ 1684: [Usaco2005 Oct]Close Encounter

    题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MB Description Lacking e ...

  2. 【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)( ...

  3. bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】

    枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...

  4. bzoj1684 [Usaco2005 Oct]Close Encounter

    Description Lacking even a fifth grade education, the cows are having trouble with a fraction proble ...

  5. bzoj1745[Usaco2005 oct]Flying Right 飞行航班*

    bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...

  6. bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)

    之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西. 这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备 ...

  7. bzoj:1685 [Usaco2005 Oct]Allowance 津贴

    Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...

  8. 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...

  9. BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...

随机推荐

  1. List与Linkedlist、Arrylist、Vector、Map应用

    1.List与LinkedList      List是数组链表     LinkedList是指针链表     选择List还是LinkedList要看你的使用特点.       数组链表访问快,复 ...

  2. Java线程:线程状态

    线程可以创建6状态: New()新创建.Runnable(可运行).Blocked(被阻塞). Waiting(等待).Timed waiting(计时等待).Terminated(被终止)1 新建线 ...

  3. easyUI panel组件

    easyUI panel组件: 属性的使用: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  4. Spring mvc 数据验证

    加入jar包 bean-validator.jar 在实体类中加入验证Annotation和消息提示 package com.stone.model; import javax.validation. ...

  5. 在Flex中定义移动设备应用程序和启动屏幕

    创建移动设备应用程序容器 移动设备应用程序中的第一个标签通常是以下标签之一: <s:ViewNavigatorApplication> 标签用于定义只有一个部分的移动设备应用程序. < ...

  6. MySQL各存储引擎

    MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术,你能够获得额外的速度或 ...

  7. C# 连接 SQLServer 及操作

    随笔:连接: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=d ...

  8. [css]《css揭秘》学习(二)-多重边框

    一.box-shadow属性 box-shadow除了生成投影,还可以用来生成边框:它接受第4个值作为扩张半径,0的x轴偏移+0的y轴偏移+0的模糊值+0的扩张半径,得到的显示效果和边框是一样的:但是 ...

  9. 第七届蓝桥杯javaB组真题解析-凑算式(第三题)

    题目 /* 凑算式 B DEF A + --- + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字. 比 ...

  10. 制作jar文件

    一.制作可运行jar文件 使用java的swing.awt制作了一个简单的界面交互模块.程序打成jar包后,能双击运行,制作过程: 1.eclipse →properties →Export,选择ja ...