uva 10555

來源:互聯網
上載者:User

題目連結:uva 10555 - Dead Fraction

題目大意:給出一個小數,從...開始可以是任何數字,但是保證是無限迴圈小數,將該小數用分式的形式表示,並且要求分母盡量大。

解題思路:這題主要是怎麼將無限迴圈小數轉換成分式,這樣的:

  • 有小數0.abcdEEE,未迴圈部分長4,迴圈節為E,E的長度為i(假設)
  • abcd+E999…(i位9)10i
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 105;const ll INF = 0x3f3f3f3f3f3f3f;char s[maxn];ll gcd (ll a, ll b) {    return b ? gcd(b, a%b) : a;}int main () {    while (scanf("%s", s) == 1 && strcmp(s, "0")) {        int len = strlen(s)-5;        ll ansu, ansd = INF;        for (int i = 0; i < len; i++)            s[i] = s[i+2];        for (int i = 0; i < len; i++) {            ll d = 1, u = 0;            for (int j = 0; j < i; j++) {                d = d * 10;                u = u * 10 + s[j] - '0';            }            ll x = 0, y = 0;            for (int j = i; j < len; j++) {                x = x * 10 + s[j] - '0';                y = y * 10 + 9;            }            d = d * y;            u = u * y + x;            ll g = gcd(d, u);            u /= g;            d /= g;            if (d < ansd) {                ansd = d;                ansu = u;            }        }        printf("%lld/%lld\n", ansu, ansd);    }    return 0;}

相關文章

聯繫我們

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