【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 --------------------- ...
随机推荐
- hdu 1087 简单dp
思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<strin ...
- 页面刷新 vuex 数据重新被初始化
1.原因 vuex里用来存储的也只是一个全局变量,当页面刷新,该全局变量自然不存在了. 2.解决 使用localStorage存储一份 (1)storage.js /** * vuex localSt ...
- 安卓平台ARM Mali OpenCL例子-灰度转换(转)
手头一块RK3288的板子,在板子上测试了一张1080p的彩色图灰度转换的OpenCL例子.OpenCL没有任何优化.例子请移步这里. 该例子是编译成安卓平台下的可执行程序. 进入jni文件夹,进行如 ...
- 51CTO 资料汇总 截止20150504
================帖子列表,请大家选择自己喜欢的汇总贴分享================ 考试认证: 1.备战2014软考!精品视频教程推荐(综合复习+经验分享+考前冲刺)[随时更新] ...
- hdu 4742 Pinball Game 3D(三维LIS&cdq分治&BIT维护最值)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Docker实战(五)编写Dockerfile
一.创建Dockerfile文件 首先,需要创建一个目录来存放 Dockerfile 文件,目录名称可以任意,在目录里创建Dockerfile文件: 二.Dockerfile 基本框架 Dockerf ...
- CRC文件解压缩问题
CRC问题一般有三种可能1.你的硬盘出现坏道2.你的硬盘数据线受损3.还可能是主板和内存的问题 硬盘坏道的表现硬盘使用久了就可能出现各种各样的问题,而硬盘“坏道”便是这其中最常见的问题.硬盘出现坏道除 ...
- python--标准库 时间与日期 (time, datetime包)
Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wall clock time),这个时间是从某个固定时间起点到现在的时间间隔.时间起点的选择与计算机相关,但一台计算机的 ...
- MFC的CListCtrl双击获取单元格的行列
首先要把ClistCtrl设置为整选中模式和网格模式 SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 重写方法如下: void CAl ...
- java语言国际化--ResouceBundle、struts
一.Java国际化 我们使用java.lang.Locale来构造Java国际化的情境. java.lang.Locale代表特定的地理.政治和文化.需要Locale来执行其任务的操作叫语言环境敏感的 ...