【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)(a/b)*i和(llf)(a/b)*(llf)i
这两货竟然不通????上边的能对,下边的就错了??
噗。
全部都要。。(llf)a/(llf)b*(llf)i。。。。。
这样才不会错。。
T_T
教训吸取了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
typedef long double llf;
llf eps=1e30;
int a, b, ans1, ans2;
void cal(int x, int y) {
if(a*y==b*x) return;
llf c=abs((llf)x/y-(llf)a/b);
if(c<eps) {
eps=c;
ans1=x; ans2=y;
}
}
int main() {
a=getint(), b=getint();
for1(i, 1, 32767) {
int j=(llf)a/(llf)b*(llf)i;
cal(j, i); cal(j+1, i);
}
printf("%d %d", ans1, ans2);
return 0;
}
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
【BZOJ】1684: [Usaco2005 Oct]Close Encounter(暴力+c++)的更多相关文章
- BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MB Description Lacking e ...
- 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[ ...
- 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 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )
一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...
随机推荐
- 自己主动化測试使用mybatis更新数据库信息实例
代码例如以下: mybatis配置文件: <? xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- [转]K近邻算法
什么是K近邻算法 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1时,算法便成了最近邻算法,即 ...
- Java中try catch finally的执行顺序问题
finally 语句块是在 try 或者 catch 中的 return 语句之前执行的.更加一般的说法是,finally 语句块应该是在控制转移语句之前执行,控制转移语句除了 return 外,还有 ...
- 移动通信安全——GSM安全体系
一.GSM网络概述 1.安全机制 认证用户,防止未授权接入 对空中接口传输加密,防止无线信道上用户信息被窃听 SIM卡独立于终端,管理用户信息 在空中接口上以临时身份标识用户,防止用户被跟踪 但是GS ...
- 解析theme()
drupal_render()只是对theme()的调用做了包装,真正做任务的还是theme(). function theme($hook, $variables = array()) { ... ...
- Android 弹幕效果开发案例
概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂移移 ...
- 执行pig出错Unhandled internal error. Found interface jline.Terminal, but class was expected
执行pig时报例如以下错误 2015-07-14 10:41:12,869 [main] ERROR org.apache.pig.Main - ERROR 2998: Unhandled inter ...
- 停掉一台服务器,Nginx响应慢(转载)
测试发现的问题及解决办法 1.当后端两台IIS应用服务器都正常时,访问速度非常快,查看日志,原来一个请求,是后端两台服务器同时响应的; 2.为了模仿故障测试,停掉一台IIS应用服务器,这时再访问,请求 ...
- mysql启动错误之mysql启动报1067错误如何解决
MYSQL启动报1067错误,系统日志中是“服务 mysql 意外停止” Mysql日志中则是:Plugin 'FEDERATED' is disabled. 解决方法: 1.在MY.INI文件中的 ...
- 分布式ID生成方案
系统唯一ID是设计一个系统的时候常常会遇到的问题,也常常为这个问题而纠结. 生成ID的方法有很多,适应不同的场景.需求以及性能要求.所以有些比较复杂的系统会有多个ID生成的策略. 0. 分布式ID要求 ...