POJ 1650 Integer Approximation (追赶法求解)
来源:互联网 发布:淘宝女童模特 编辑:程序博客网 时间:2024/06/05 16:19
Integer Approximation
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5458 Accepted: 1806
Description
The FORTH programming language does not support floating-point arithmetic at all. Its author, Chuck Moore, maintains that floating-point calculations are too slow and most of the time can be emulated by integers with proper scaling. For example, to calculate the area of the circle with the radius R he suggests to use formula like R * R * 355 / 113, which is in fact surprisingly accurate. The value of 355 / 113 ≈ 3.141593 is approximating the value of PI with the absolute error of only about 2*10-7. You are to find the best integer approximation of a given floating-point number A within a given integer limit L. That is, to find such two integers N and D (1 <= N, D <= L) that the value of absolute error |A - N / D| is minimal.
Input
The first line of input contains a floating-point number A (0.1 <= A < 10) with the precision of up to 15 decimal digits. The second line contains the integer limit L. (1 <= L <= 100000).
Output
Output file must contain two integers, N and D, separated by space.
Sample Input
3.1415926535897910000
Sample Output
355 113
题意:在L的范围内寻找两个数N ,D,使得fabs(A-N/D)最小
思路:看到别人用的追赶法,看了一下解释,写了一下,复杂度O(n)
总结:也想过二分枚举,但是T了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 60100#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define mod 1000000007using namespace std;double a;double dis(int n,int d){return a-(double)n/d;}int main(){while(scanf("%lf",&a)!=EOF){int l;scanf("%d",&l);int d=1,n=1;int ansd=d,ansn=n;double mi=INF*1.0;while(d<=l&&n<=l){double k=dis(n,d);//printf("%lf\n",k);double kk=fabs(k);if(kk<mi){mi=kk;ansd=d;ansn=n;}if(k>0)n++;elsed++;}printf("%d %d\n",ansn,ansd);}return 0;}
0 0
- POJ 1650 Integer Approximation (追赶法求解)
- poj 1650 Integer Approximation “追赶法”搜索
- POJ1650 Integer Approximation(“追赶法”搜索)
- Poj 1650 & Zoj 1601 Integer Approximation (追赶法,分数逼近实数)
- 追赶法-ZOJ-1601-Integer Approximation
- poj 1650 Integer Approximation
- POJ 1650 Integer Approximation 笔记
- 求解线性方程组--追赶法
- POJ 1650,ZOJ 1601追赶法
- poj 1650/3039 追赶法枚举
- 求解三对角方程组的追赶法(Matlab程序)
- 追赶法求解三对角方程组
- 追赶法求解三对角方程组
- ZOJ1601:Integer Approximation
- poj 3039 Close Encounter “追赶法”搜索
- 数值分析 追赶法求解三对角线性方程组 MATLAB实现
- 追赶法
- 追赶法
- python中的lib的一些安装方法
- Android进阶——安卓接入微信,获取OpenID
- leetcode之Convert Sorted Array to Binary Search Tree
- NSData有值,NSData转换成NSString为nil
- Windows系统下查看JDK的版本和安装路径!
- POJ 1650 Integer Approximation (追赶法求解)
- C++使用libcurl做HttpClient
- jQuery.makeArray() 函数详解
- 输入数据,求平均值,输出比平均值大的数据和位置,并统计个数。
- leetcode之Convert Sorted List to Binary Search Tree
- android应用程序窗口框架学习(2)-view绘制流程源代码解析-setContentView与LayoutInflater加载解析机制源码分析
- 29. Divide Two Integers leetcode python new season 2016
- 阿里云centOS 将不带3www的域名301重定向到带www域名
- windows的体系结构