Problem - 2225

  一道简单数学题,要求求出一个分母不超过m的最接近sqrt(n)的分数。

  做法就是暴力枚举,注意中间过程不能用浮点数比较,误差要求比较高。

代码如下:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath> using namespace std; typedef long long LL;
template<class T> T sqr(T x) { return x * x;}
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;} int main() {
LL n, m;
while (cin >> n >> m) {
LL bx = , by = ;
// long double r = sqrt((long double) n);
// cout << r << endl;
for (LL i = ; i <= m; i++) {
LL t = (int) sqrt((double) sqr(i) * n);
for (int d = ; d < ; d++) {
// if (fabs((long double) (t + d) / i - r) < fabs((long double) bx / by - r)) {
// bx = t + d, by = i;
// }
if (abs(sqr(t + d) * sqr(by) - n * sqr(by) * sqr(i)) < abs(sqr(bx) * sqr(i) - n * sqr(by) * sqr(i))) bx = t + d, by = i;
}
}
LL GCD = gcd(bx, by);
cout << bx / GCD << "/" << by / GCD << endl;
}
return ;
}

——written by Lyon

hdu 2225 The nearest fraction (数学题)的更多相关文章

  1. Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分

    B. Nearest Fraction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...

  2. hdu 1299 Diophantus of Alexandria(数学题)

    题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可 ...

  3. HDU 1018 Big Number (数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...

  4. HDU 2529 Shot (物理数学题)

    题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...

  5. HDU 5584 LCM Walk(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...

  6. hdu 4970 Killing Monsters(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 Problem Description Kingdom Rush is a popular TD ...

  7. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

  8. hdu 4342 History repeat itself(数学题)

    题目链接:hdu 4342 History repeat itself 题意: 让你找第a个非完全平方数m,并且求前m个数的开方向下取整的和. 题解: 第一个问题: 假设第a个非平方数是X,X前面有n ...

  9. HDU 1017 A Mathematical Curiosity 数学题

    解题报告:输入两个数,n和m,求两个数a和b满足0<a<b<n,并且(a^2+b^2+m) % (a*b) =0,这样的a和b一共有多少对.注意这里的b<n,并不可以等于n. ...

随机推荐

  1. Zookeeper 扫盲

    Zookeeper 扫盲 :disappointed_relieved: 配置文件详解: tickTime:基本事件单元,以毫秒为单位,这个时间作为 Zookeeper 服务器之间或客户端之间维持心跳 ...

  2. 计蒜客 Prefix Free Code(字典树+树状数组)

    Consider n initial strings of lower case letters, where no initial string is a prefix of any other i ...

  3. windows下maven的安装配置

    什么是maven Maven是基于POM(工程对象模型),通过一小段描述来对项目的代码.报告.文件进管理的工具. Maven是一个跨平台的项目管理工具,它是使用java开发的,它要依赖于jdk1.6及 ...

  4. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  5. React map生成元素添加点击事件绑定this

    问题 使用.map(function(Item)生成元素添加onClick事件:onClick={this.provinceChange.bind(this, "99")}时,前台 ...

  6. Duplicate a whole line in Vim

    yy or Y to copy the line or dd to delete (cutting) the line then p to paste the copied or deleted te ...

  7. phpstorm配置Xdebug进行调试PHP教程_php技巧_脚本之家

    运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xdebug-2.2.5-5.6-vc11-x86_64.dll ps : php版本和xdeb ...

  8. Mac上代码开启dump的core文件生成方案

    #ifdef Q_OS_MAC struct rlimit rl; getrlimit(RLIMIT_NOFILE,&rl); rl.rlim_cur = qMin((rlim_t)OPEN_ ...

  9. AlertDialog提示对话框练习

    闲来无事,就练习了下AlertDialog对话框. 首先写了三个button,分别打开一般对话框,单选列表对话框和复选对话框. xml代码 <LinearLayout xmlns:android ...

  10. 【JZOJ4934】【NOIP2017GDKOI模拟1.12】a

    helpless fucking 结论:如果一个数可以被对于a序列中每个数的最大公约数整除,那么它就是好的. Bitch Man 感性证明: 贪心地想,对于a序列中的任意两个数,它们的最大公约数可由这 ...