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((double)x/y-(double)a/b);
        aa=x,ab=y;
    }
}
int main()
{
    scanf("%d%d",&a,&b);
    for(int i=1;i<=32767;i++)
        wk(floor((double)a/b*i),i),wk(floor((double)a/b*i)+1,i);
    printf("%d %d\n",aa,ab);
    return 0;
}

原文地址:https://www.cnblogs.com/lokiii/p/9159405.html

时间: 2024-10-06 20:05:21

bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】的相关文章

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

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 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )

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

BZOJ 3408: [Usaco2009 Oct]Heat Wave 热浪( 最短路 )

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