Best Rational Approximation( 法裡數列)

來源:互聯網
上載者:User

標籤:reason   \n   tin   return   ble   輸入   狀態   記憶體   div   

Best Rational Approximation時間限制: 3 Sec  記憶體限制: 128 MB
提交: 297  解決: 25
[提交][狀態][討論版][命題人:admin]題目描述Many microcontrollers have no floating point unit but do have a (reasonably) fast integer divide unit. In these cases it may pay to use rational values to approximate floating point constants. For instance,  
355/113 = 3.1415929203539823008849557522124  
is a quite good approximation to  
π = 3.14159265358979323846  
A best rational approximation, p/q, to a real number, x, with denominator at most M is a rational number, p/q (in lowest terms), with q <= M such that, for any integers, a and b with b <= M, and a and b relatively prime, p/q is at least as close to x as a/b:  
|x – p/q|≤|x – a/b|  
Write a program to compute the best rational approximation to a real number, x, with denominator at most M. 輸入The first line of input contains a single integer P, (1≤P≤1000), which is the number of data sets that follow.  Each data set should be processed identically and independently.  
Each data set consists of a single line of input.  It contains the data set number, K, followed by the maximum denominator value, M (15≤M≤100000), followed by a floating-point value, x, (0≤x < 1). 輸出For each data set there is a single line of output.  The single output line consists of the data set number, K, followed by a single space followed by the numerator, p, of the best rational approximation to x, followed by a forward slash (/) followed by the denominator, q, of the best rational approximation to x. 範例輸入
31 100000 .1415926535897932382 255 .1415926535897932383 15 .141592653589793238
範例輸出
1 14093/995322 16/1133 1/7
提示 來源

Greater New York Regional Contest 2017 

 

 

c++ code:

#include <bits/stdc++.h>using namespace std;int main(){    int p,t,m;    double x;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%lf",&p,&m,&x);        int  a=0,b=1,c=1,d=1,e,f;        while(true)        {            e=a+c;            f=b+d;            int gcd=__gcd(e,f);            e/=gcd;f/=gcd;            if(f>m) break;            if(1.0*e/f<=x)            {                a=e;b=f;            }            else            {                c=e;d=f;            }        }        printf("%d ",p);        if(fabs(1.0*a/b-x)>fabs(1.0*c/d-x))            printf("%d/%d\n",c,d);        else            printf("%d/%d\n",a,b);    }    return 0;}

 

Best Rational 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.