bzoj1684 [Usaco2005 Oct]Close Encounter
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.
题意是给定一个分数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的更多相关文章
- BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MB Description Lacking e ...
- 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)( ...
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- bzoj1745[Usaco2005 oct]Flying Right 飞行航班*
bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...
- bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)
之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西. 这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备 ...
- 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://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
- BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...
随机推荐
- 比较有用的log4j.properties
转自 http://walsh.iteye.com/blog/314941 log4j.properties log4j.rootLogger=DEBUG,CONSOLE,DATABASE,FIL ...
- hdu 5433 Xiao Ming climbing(bfs+三维标记)
Problem Description Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can ...
- poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- mysql增量备份 percona-xtrabackup
先说下实际环境 阿里云买的ESC跑的mysql服务,目前由于数据量过大,数据库目前有800多GB,每次全备需要等20多个小时才能够完成,然后就想到做增量备份,写下此文档 一.测试环境 [root@lo ...
- 高性能WEB开发(6) - web性能測试工具推荐
WEB性能測试工具主要分为三种.一种是測试页面资源载入速度的,一种是測试页面载入完成后页面呈现.JS操作速度的,另一种是整体上对页面进行评价分析,以下分别对这些工具进行介绍,假设谁有更好的工具也请一起 ...
- Android应用程序启动过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程 ...
- mysql的“Got error 28 from storage engine”错误
磁盘临时空间不够导致.解决办法:清空/tmp目录,或者修改my.cnf中的tmpdir参数,指向具有足够空间目录
- android——ImageLoader添加缓存
//给图片加入到缓存中. DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc( ...
- 经典SQL语句大全(转载)
原文http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html#top 一.基础 1.说明:创建数据库CREATE DATABAS ...
- 加载为应用程序池‘DefaultAppPool'提供服务的进程失败,应用程序池被禁用【解决方法】
iis应用程序池不能启动2008年03月13日 星期四 15:29iis应用程序池不能启动.WINDOWS2003 ENT SERVER(64位)环境下(测试过32位系统一样操作无此问题),当打开“应 ...