题目

1684: [Usaco2005 Oct]Close Encounter

Time Limit: 5 Sec  Memory Limit: 64 MB

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

题解

这道题就是看你细不细心= =就是数据类型转换的问题,还有必须要用long double~

代码

 /*Author:WNJXYK*/
#include<cstdio>
using namespace std;
int n,m;
long double tmp;
int ansb,ansa;
long double delta=1e30;
inline long double abs(long double x){
if (x<) return -x;
return x;
}
int main(){
scanf("%d%d",&n,&m);
tmp=(long double)n/(long double)m;
for (int b=;b<=;b++){
int fz=((long double)n/(long double)m*(long double)b);
if (abs((long double)(fz-)/(long double)b-(long double)n/(long double)m)<delta && (fz-)*m!=n*b){
delta=abs((long double)(fz-)/(long double)b-(long double)n/(long double)m);
ansa=fz-;
ansb=b;
}
if (abs((long double)(fz)/(long double)b-(long double)n/(long double)m)<delta && fz*m!=n*b){
delta=abs((long double)(fz)/(long double)b-(long double)n/(long double)m);
ansa=fz;
ansb=b;
}
if (abs((long double)(fz+)/(long double)b-(long double)n/(long double)m)<delta && (+fz)*m!=n*b){
delta=abs((long double)(fz+)/(long double)b-(long double)n/(long double)m);
ansa=fz+;
ansb=b;
}
}
printf("%d %d\n",ansa,ansb);
return ;
}

BZOJ 1684: [Usaco2005 Oct]Close Encounter的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  9. Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜

    1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 122  So ...

随机推荐

  1. frame.origin.x 的意思和作用?

    frame.origin.x 的意思和作用? scrollView.frame 一个view的frame 包含它的矩形形状(size)的长和宽. 和它在父视图中的坐标原点(origin)x和y坐标 f ...

  2. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  3. Matlab中的取整-floor,ceil,fix,round

    FLOOR Round towards minus infinity. FLOOR(X) rounds the elements of X to the nearest integers toward ...

  4. .Net Web开发中实现剪切板功能

    我要实现的功能是:在列表页,通过一个按钮复制对应的文章Url,如下图: 如下代码:     <a class="btn btn-success copy" href=&quo ...

  5. What I Have Lived For(我为什么而活着-罗素)

    What I Have Lived For by Bertrand Russell Three passions, simple but overwhelmingly strong, have gov ...

  6. [转]使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单

    原文链接:使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单 这个留待后面玩一下,还是有安全隐患,非核心业务 临时用一下可以. 设置防火墙策略时,关于SSH:22访问权限, ...

  7. HTML+CSS笔记 CSS笔记集合

    HTML+CSS笔记 表格,超链接,图片,表单 涉及内容:表格,超链接,图片,表单 HTML+CSS笔记 CSS入门 涉及内容:简介,优势,语法说明,代码注释,CSS样式位置,不同样式优先级,选择器, ...

  8. JavaScript闭包理解的关键 - 作用域链

    阮一峰的一篇文章已经对闭包的用途.概念讲解地相对清晰了. 闭包就是能够读取其他函数内部变量的函数. 但我认为里面对于作用域链的解释还不够清晰,这里作一些补充. 闭包之所以可以读取外部函数的内部变量,即 ...

  9. Java中volatile的作用以及用法

    volatile让变量每次在使用的时候,都从主存中取.而不是从各个线程的“工作内存”. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正 ...

  10. java程序获得SqlServer数据表的表结构

    /**   * 取得一张表的结构信息   * 使用DatabaseMetaData与ResultSetMetaData结合的方式获取全部属性   * @param conn   数据连接   * @p ...