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 ...
随机推荐
- 最近用的到的一些js的常用方法(简单的)
由于新的项目开始了,是使用MVC 5.0 开发的,前端使用了两个主流的框架 UIKIT,Ignite UI(收费) 因为是mvc主要用json来交互,不能避免要对前端脚本进行操作,所以就将能用到的方法 ...
- WPF之DataGrid应用(转)
原文:http://blog.csdn.net/sanjiawan/article/details/6785394 前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功 ...
- ActiveMQ下载及安装
1.下载ActiveMQ 官方网站:http://activemq.apache.org/ 根据需要下载不同的版本.我下载的是5.13.3-win64的版本 2.运行ActiveMQ服务 2.1解压缩 ...
- Lucence.Net学习+盘古分词
创建索引库 //读取文件,存储到索引库 public string CreateDatebase() { //获取索引库的路径 ...
- HDU2008-数值统计
描述: 统计给定的n个实数中,负数.零和正数的个数. 代码: #include<stdio.h> #include<string.h> #include<iostream ...
- 【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
安装完ASP.NET,Oracle9i客户端后,使用System.Data.OracleClient访问Oracle数据库如果出现这种错误:System.Data.OracleClient requi ...
- javascriptDOM编程艺术_学习笔记_知识点 动态创建标记
传统技术:document.write 和 innerHTML 深入剖析DOM方法:createElement.createTextNode.appendChild 和 insertBefore ...
- JavaScript 验证提交文件的信息
前言 目前工作任务终于告一段落了,今天发现之前写的文件上传的代码有点小瑕疵,就是上传图片如果超过 2M 就会出错,因为七牛云好像限制了上传图片的大小,所以就用 JavaScript 在文件选中之后,上 ...
- A Byte of Python 笔记(4)控制流:if、for、while、break、continue
第6章 控制流 3种控制流语句-- if for while 默认pyhon使用ASCII码来解释程序的,默认不支持中文,需要在程序的第一行或者第二行声明编码.官方参考具体参考以下三种方式:1. ...
- 理解Python的with as语句
简单的说, with open(filepath, 'wb') as file: file.write("something") 等价于: file = open(filepath ...