BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目
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
Sample Output
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的更多相关文章
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- 1684: [Usaco2005 Oct]Close Encounter
1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 387 Solved: 181[ ...
- 【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)( ...
- bzoj1684 [Usaco2005 Oct]Close Encounter
Description Lacking even a fifth grade education, the cows are having trouble with a fraction proble ...
- 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://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...
- bzoj1745[Usaco2005 oct]Flying Right 飞行航班*
bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
- Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜
1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 122 So ...
随机推荐
- list-style:none outside none;的作用
今天在论坛里面看到一篇文章,讲的是以前忽略的一个问题.就是当ul里面有float和display:inline,在ie6.ie7里面会有一些问题.一般对ul进行reset也好,或是设置ul的样式时,往 ...
- MVC过滤器进行统一登录验证
统一登录验证: 1.定义实体类Person:利用特性标签验证输入合法性设计登录页面 1 2 3 4 5 6 7 8 9 public class Person { [DisplayName(& ...
- 替换Avada主题的Google字体
刚玩WP的时候图省事,在themeforest买了排行第一的主题Avada,虽然强大,但对我目前的Blog应用而言实在太'重'了.而且老外的主题很多方面不接地气,比如谷歌字体.本文指导各位如何在Ava ...
- java动手动脑课后思考题
public class SquareInt { public static void main(String[] args) { int result; ; x <= ; x++) { res ...
- HTML+CSS笔记 CSS中级 一些小技巧
水平居中 行内元素的水平居中 </a></li> <li><a href="#">2</a></li> &l ...
- Ubuntu 12.04中文输入法的安装(转)
Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...
- RESTClient 控件 从服务器获得数据集 REST
用TRESTClient控件调用REST架构服务 RESTClient控件返回数据集 用到的控件 RESTClient RESTRequest RESTResponseDataSetAdapter p ...
- C++的二进制兼容问题(以QT为例)
二进制不兼容带来的问题(需要重新编译库文件,以前编译的失效): http://my.oschina.net/lieefu/blog/505363?fromerr=f5jn7rct 二进制不兼容的原理: ...
- JetBrains IntelliJ IDEA for Mac 15.0 破解版 – Mac 上强大的 Java 集成开发工具
应网友要求更新. IntelliJ IDEA 是最强大的 Java IDE 之一,由知名的Jetbrainsg公司出品,最新版本增加了大量强大易用的特性,比如 Java 8 的Lambda 表达式调试 ...
- Nginx CORS实现JS跨域
1. 什么是跨域 简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象. 同源是指相同的协议.域名.端口.特别注意两点: 如果 ...