POJ 1650 Integer approximation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.