【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

2 3

Sample Output

21845 32767

OUTPUT DETAILS:

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

HINT

Source

Silver

时间: 2024-10-28 16:37:37

【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 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

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

枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cmath> using namespace std; int a,b,aa,ab; double mx=10; void wk(int x,int y) { if(x*b==y*a) return; if(fabs((double)x/y-(double)a/b)<mx) { mx=fabs((dou

1684: [Usaco2005 Oct]Close Encounter

1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 181[Submit][Status][Discuss] Description Lacking even a fifth grade education, the cows are having trouble with a fraction problem from their textbook. Pl

bzoj1684[Usaco2005 Oct]Close Encounter*

bzoj1684[Usaco2005 Oct]Close Encounter 题意: 找一个分数它最接近给出一个分数.你要找的分数的分子分母的范围在1..32767. 题解: 枚举所求分数的分子,用其乘上给出分数得到一个浮点数分母,比较分母向上/下取整所得分数与答案比较. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #defi

BZOJ 3407: [Usaco2009 Oct]Bessie&#39;s Weight Problem 贝茜的体重问题( dp )

01背包... ----------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define clr( x , c ) m

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

bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到农场1,上面有c个座位,问最多可以满足多少只牛的要求.n≤10000,k≤50000,c≤100. 题解: 用类似贪心的方法做,现将每个农场出发的牛组织成链表.先求早上:当飞机到达每个农场时,先让到达的奶牛下机,接着如果飞机未满,则将其填满,之后枚举剩下的奶牛,如果它们的目的地比坐在飞机上面的奶牛目

BZOJ(begin) 1333 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】

题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为val,数量为cnt. 每个月你要给Bessie发金额为c的津贴(可以比c多,但不能少). 问你最多能发多少个月. 题解: 贪心. 贪心策略: (1)如果能恰好凑出c的钱,则应尽可能使用大币值的硬币. (2)如果不能恰好凑出,则应让花的冤枉钱尽可能少. 实现: 先按币值从大到小排序... (1)在保

BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理

题目 1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec  Memory Limit: 64 MB Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <

BZOJ 1709: [Usaco2007 Oct]Super Paintball超级弹珠

题目 1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 329  Solved: 255[Submit][Status] Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩弹游戏设备(类乎于真人版CS). Bessie把她们玩游戏草坪划成了N * N(1 <= N<= 100)单位的矩阵,同时列出了她的 K (1 <= K &l