Integer approximation
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 5326 |
|
Accepted: 1750 |
Description
The FORTH programming language does not support floating-point arithmetic @ all. Its author, Chuck Moore, maintains, floating-point calculations is too slow and most of the time can is emulated by I Ntegers 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, whic H is in fact surprisingly accurate. The value of 355/113≈3.141593 is approximating the value of a PI with the absolute error of only about 2*10-7. You is to find the best integer approximation of a given floating-point number a within a given integer limit L. That's, to find such-integers n and D (1 <= N, D <= L) that's 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 < Ten) with the precision of up to the decimal di Gits. The second line contains the integer limit L. (1 <= L <= 100000).
Output
Output file must contain-integers, N and D, separated by space.
Sample Input
3.1415926535897910000
Sample Output
355 113
Source
Northeastern Europe 2001, Far-eastern subregion
Analysis:
Enumeration
1#include <iostream>2 using namespacestd;3 intMain () {4 Doublea,min=100005;5 intN;6Cin>>a>>N;7 Doubleb,c;8c=b=1;9 intp=1, q=1;Ten while(c<=n&&b<=N) { One if(c/b>a) { A if(c/b-a<min) { -min=c/b-A; -p=C; theq=b; - } -b++; - } + Else{ - if(a-c/b<min) { +min=a-c/b; Ap=C; atq=b; - } -C++; - } - } -cout<<p<<" "<<q<<Endl; in return 0; -}
POJ 1650 Integer approximation