24h購物| | PChome| 登入
2012-12-23 18:52:40| 人氣1,562| 回應0 | 上一篇 | 下一篇

[UVA][分數循環] 10555 - Dead Fraction

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B: Dead Fraction

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by "...". For instance, instead of "1/3" he might have written down "0.3333...". Unfortunately, his results require exact fractions! He doesn't have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.

To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).

There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case. For each case, output the original fraction.

Note that an exact decimal fraction has two repeating expansions (e.g. 1/5 = 0.2000... = 0.19999...).

Sample Input

0.2...
0.20...
0.474612399...
0

Output for Sample Input

2/9
1/5
1186531/2500000


此題給一個循環小數,但沒給你後面的循環長度,自行劃分找到一個分數表示法產生這個表示法,
輸出一個分母最小的分數。


#include <stdio.h>
#define LL long long
LL gcd(LL x, LL y) {
    LL t;
    while(x%y)
        t = x, x = y, y = t%y;
    return y;
}
LL mpow(LL x, LL y) {
    if(y == 0)  return 1;
    if(y&1) return x*mpow(x*x, y>>1);
    return mpow(x*x, y>>1);
}
int main() {
    char s[105];
    int i;
    while(scanf("%s", s) == 1) {
        if(s[1] == '\0')
            break;
        for(i =  2; s[i]; i++)
            if(s[i] == '.')
                break;
        s[i] = '\0';
        int a = i-2, t;
        LL x, y, ax, ay, g, tx, ty;
        y = mpow(10, a)-1;
        sscanf(s+2, "%lld", &x);
        g = gcd(x, y);
        ax = x/g, ay = y/g;
        for(i = 2; i < a+1; i++) {
            t = s[i+1];
            s[i+1] = '\0';
            sscanf(s+2, "%lld", &x);
            s[i+1]  = t;
            sscanf(s+i+1, "%lld", &y);
            ty = mpow(10, i-1)*(mpow(10, a-i+1)-1);
            tx = x*(mpow(10, a-i+1)-1)+y;
            g = gcd(tx, ty);
            tx /= g, ty /= g;
            if(ty < ay) ax = tx, ay = ty;
            //printf("%lld %lld %d %d %lld/%lld\n", x, y, i-1, a-i+1, tx, ty);
        }
        printf("%lld/%lld\n", ax, ay);
    }
    return 0;
}

台長: Morris
人氣(1,562) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二分搜+KMP] 12467 - Secret Word
此分類上一篇:[UVA][dp] 11569 - Lovely Hint

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文