cf#172div2 符合要求最近的分數

來源:互聯網
上載者:User
/*B題,題意:給你x,y,n; a(a>=0),b(b<=n),要你找出離 x/y 最近的分數  a/b 。     如果存在多個結果,則取分母最小的,如果還有多個結果,則取分子最小的解析:從 1 到 n 的分母進行遍曆,然後對每一個分母找出最接近的分子,其中分子不需要每次從0開始算。。。       小學知識:分子相同,分母大的反而小。。。。 */#include<iostream>#include<cstdio>#include<cmath>using namespace std;int main(){    double x,y,n;    while(cin>>x>>y>>n){        double p=0,q=1,a=0,b=1,ans=99999999;        while(b<=n){            while(abs(a/b-x/y)>abs((a+1)/b-x/y))                a++;            if(abs(a/b-x/y)<ans){                p=a; q=b; ans=abs(a/b-x/y);            }            if(ans==0) break;                b++;        }        cout<<p<<"/"<<q<<endl;       }}

/*A 題題意:給你字串,第一個字母一定要是大寫的形式輸出。。 */ #include<iostream>#include<cstdio>#include<string>#include<algorithm>#define manx 100009using namespace std;int main(){    string str;    while(cin>>str){        if(str[0]<='z'&&str[0]>='a')        str[0]=(str[0]-'a')+'A';        cout<<str<<endl;    }}/*ApPLekonjac*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.