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

2 3

Sample Output

21845 32767

OUTPUT DETAILS:

21845/32767 = .666676839503.... ~ 0.666666.... = 2/3.

HINT

Source

【BZOJ】1684: [Usaco2005 Oct]Close Encounter(暴力+c++)的更多相关文章

  1. BZOJ 1684: [Usaco2005 Oct]Close Encounter

    题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MB Description Lacking e ...

  2. bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】

    枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...

  3. 1684: [Usaco2005 Oct]Close Encounter

    1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 181[ ...

  4. bzoj1684 [Usaco2005 Oct]Close Encounter

    Description Lacking even a fifth grade education, the cows are having trouble with a fraction proble ...

  5. bzoj:1685 [Usaco2005 Oct]Allowance 津贴

    Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...

  6. BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...

  7. bzoj1745[Usaco2005 oct]Flying Right 飞行航班*

    bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...

  8. 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...

  9. BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )

    一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...

随机推荐

  1. 开发移动 APP 时,你应注意这 5 个细节

    智能手机的普及带动了大批移动应用的诞生,这些应用能够帮助人们解决日常生活所面临的种种问题.Smart Insights 发表的一份报告指出,移动应用占人们使用智能手机总时间的89%,因此,为了确保你所 ...

  2. QtGui.QLineEdit

    A QtGui.QLineEdit is a widget that allows to enter and edit a single line of plain text. There are u ...

  3. TP框架中field查询字段

    TP框架中field查询字段 不是fields 也不是files !!!! 不是fields 也不是files !!!! 不是fields 也不是files !!!! 不是fields 也不是file ...

  4. 【linux】查看内存和CPU使用情况

    1.内存命令:free 解释:以上数据单位KB. 所以,上面的mem物理内存共1G 下面是对这些数值的解释: total:总计物理内存的大小. used:已使用多大. free:可用有多少. Shar ...

  5. 在CentOS上安装Git(转)

    CentOS的yum源中没有git,只能自己编译安装,现在记录下编译安装的内容,留给自己备忘. 确保已安装了依赖的包 yum install curl yum install curl-devel y ...

  6. C#获取文件的md5

    上代码: //获取MD5 public static string GetMd5Hash(String input) { if (input == null) { return null; } MD5 ...

  7. C#:将空间数据加载到树视图控件

    自己 整理了 下 代码 测试了下 还行... #region 操作树视图控件 /// <summary> /// 自定义需要的类型 /// </summary> enum Da ...

  8. 如何将.sof转换成.jic

    因为不同版本的QUARTUS II可能界面稍有差异,因此就不做截图演示了,只说操作步骤: 1.通过综合生成包含FPGA配置数据的.sof文件 2.选择转换编程文件,菜单File->convert ...

  9. LIntcode---将二叉搜索树转成较大的树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  10. Mysql 数据库数值类型详解

    MySQL 支持所有标准SQL 中的数值类型,其中包括严格数值类型(INTEGER.SMALLINT.DECIMAL 和NUMERIC),以及近似数值数据类型(FLOAT.REAL 和DOUBLE P ...