poj 1650 Integer Approximation

來源:互聯網
上載者: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 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

Source

Northeastern Europe 2001, Far-Eastern Subregion

 

分析:

枚舉

 1 #include<iostream> 2 using namespace std; 3 int main(){ 4     double a,min=100005; 5     int n; 6     cin>>a>>n; 7     double b,c; 8     c=b=1; 9     int p=1,q=1;10     while(c<=n&&b<=n){11          if(c/b>a){12              if(c/b-a<min){13                  min=c/b-a;14                  p=c;15                  q=b;16              }17              b++;18          }19          else{20              if(a-c/b<min){21                  min=a-c/b;22                  p=c;23                  q=b;24              }25              c++;26          }    27     }28     cout<<p<<" "<<q<<endl;29     return 0;30 }

 

poj 1650 Integer Approximation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.