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.

题意是给定一个分数a/b,求和它最接近的分数x/y。只要限制1<=x,y<=32767就行了

我们从1到32767枚举分母j,然后显然和a/b最相近的分数一定是i/j和(i+1)/j。至于怎么算自己脑补一下就行,很简单

有一个问题是判定i/j和a/b是否完全相等,如果怕被卡精度可以直接判定(i*b==j*a)是否成立就行了。这也很简单,就是移项一下

然后没有了

#include<cstdio>
#include<cmath>
int a,b,i,aa,bb;
double save=10;
inline double abs(double x)
{if (x<0)x=-x;return x;}
void jud(int x,int y)
{
if (x*b==y*a) return;
double work=abs((double)x/y-(double)a/b);
if (work<save)
{
save=work;
aa=x;
bb=y;
}
}
int main()
{
scanf("%d%d",&a,&b);
for (int j=1;j<=32767;j++)
{
i=(int)floor((double)a/b*j);
jud(i,j);
jud(i+1,j);
}
printf("%d %d\n",aa,bb);
}

bzoj1684 [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. 1684: [Usaco2005 Oct]Close Encounter

    1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 181[ ...

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

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

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

  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. haporxy 负载elasticsearch

    <pre name="code" class="html">-bash-4.1# cat /etc/haproxy/haproxy.cfg glob ...

  2. 方案:在Eclipse IDE 中搭建Python开发环境

    Eclipse是一款功能强大的IDE,Python是一种功能强大的计算机语言,但是Python的IDE环境确实很缺乏,如果在强大的Eclipse中添加Python开发环境,那样就很完美了. 在这里,我 ...

  3. poj 1149 pigs(最大流)

    题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...

  4. 选择排序(Selection Sort)

    选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...

  5. Java应用开发的一条经验

    一旦为应用建立良好的基础设施, 后续的开发就会变得容易而快速.  这些基础设施包括: 1.   线程池的建立.配置: 在 JDK 并发库的基础上建立更适合于应用的并发使用接口: 2.   跨多数据源的 ...

  6. MySQL的备份和还原

    MySQL的备份和还原 备份:副本    RAID1,RAID10:保证硬件损坏而不会业务中止:        DROP TABLE mydb.tb1; 备份类型:        热备份.温备份和冷备 ...

  7. CAS原理与协议

    SSO英文全称Single Sign On,单点登录. SSO是在多个应用系统中,用户仅仅须要登录一次就能够訪问全部相互信任的应用系统. SSO的解决方式非常多,比方收费的有UTrust.惠普灵动等, ...

  8. Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8526644 在Android系统中,输入法窗口 ...

  9. CLR via C# - Char_String

    .NET中Char表示为16为的Unicode值,Char提供两个public const字段MinValue('\0',写成'\u0000'也是一样的)和MaxValue('\uffff'). Ch ...

  10. MongoDB学习笔记02

    MongoDB中使用find来进行查询,查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.find的第一个参数决定了要返回哪些文档.空的查询文档{}会匹配集合的全部内容,要是不指定查 ...