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

2 3

Sample Output

21845 32767

OUTPUT DETAILS:

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

HINT

Source

Silver

题解

这道题就是看你细不细心= =就是数据类型转换的问题,还有必须要用long double~

代码

 1 /*Author:WNJXYK*/
 2 #include<cstdio>
 3 using namespace std;
 4 int n,m;
 5 long double tmp;
 6 int ansb,ansa;
 7 long double delta=1e30;
 8 inline long double abs(long double x){
 9     if (x<0) return -x;
10     return x;
11 }
12 int main(){
13     scanf("%d%d",&n,&m);
14     tmp=(long double)n/(long double)m;
15     for (int b=1;b<=32767;b++){
16         int fz=((long double)n/(long double)m*(long double)b);
17         if (abs((long double)(fz-1)/(long double)b-(long double)n/(long double)m)<delta && (fz-1)*m!=n*b){
18                 delta=abs((long double)(fz-1)/(long double)b-(long double)n/(long double)m);
19                 ansa=fz-1;
20                 ansb=b;
21         }
22         if (abs((long double)(fz)/(long double)b-(long double)n/(long double)m)<delta && fz*m!=n*b){
23                 delta=abs((long double)(fz)/(long double)b-(long double)n/(long double)m);
24                 ansa=fz;
25                 ansb=b;
26         }
27         if (abs((long double)(fz+1)/(long double)b-(long double)n/(long double)m)<delta && (1+fz)*m!=n*b){
28             delta=abs((long double)(fz+1)/(long double)b-(long double)n/(long double)m);
29             ansa=fz+1;
30             ansb=b;
31         }
32     }
33     printf("%d %d\n",ansa,ansb);
34     return 0;
35 }

时间: 2024-11-08 22:29:30

BZOJ 1684: [Usaco2005 Oct]Close Encounter的相关文章

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

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

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 3408: [Usaco2009 Oct]Heat Wave 热浪( 最短路 )

普通的最短路...dijkstra水过.. ------------------------------------------------------------------------------ #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<queue> #define rep( i , n ) for( int i =

[BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MaxN = 200 + 5,