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 ...
随机推荐
- IE7下浮动元素的内容自动换行的BUG解决方法
有时候我们想写个浮动得到这样的效果: 代码: <!doctype html> <html> <head> <meta charset="utf-8& ...
- margin 属性的相关问题
1.margin 的IE6 双边距问题 问题描述:浮动的块挨边框的时候会产生双倍的边距 解决方案: 1.增加display:inline; 2.去除float属性 2.margin 的重叠问题 CSS ...
- window.parent与window.opener的区别与使用
window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...
- BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )
dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...
- java 多个文件打包zip
/** * 多个文件打包成zip */ public class ZipDemo { private static void create() throws Exception{ String pat ...
- fiddler---使用方法1--抓取手机app包
1.首先fiddler设置
- android-适配Adapter
Adapter是把数据和用户界面视图绑定到一起的桥梁类,负责创建用来表示父视图中的每一个条目的子视图,并提供对底层数据的访问. public class MainActivity extends Ac ...
- (iOS)推送常见问题
1.为什么启动的时候出现 Did Fail To Register For Remote Notifications With Error的错误程序运行的时候出现下面的错误信息: did Fail T ...
- 激活Windows 10 正式版
原文 http://jingyan.baidu.com/article/27fa732684b5f646f8271ff4.html Windows 10只提供为期一年的免费升级.因此,不要无限拖延期自 ...
- php中 $$str 中 "$$" 的解释
原文:php中 $$str 中 "$$" 的解释 这种写法称为可变变量有时候使用可变变量名是很方便的.就是说,一个变量的变量名可以动态的设置和使用.一个普通的变量通过声明来设置,例 ...