[UVA][分循] 10555 - Dead Fraction@Morris' Blog|PChome Online 人新台
2012-12-23 18:52:40| 人1,691| 回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);
 nbsp;          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,691) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][二分搜+KMP] 12467 - Secret Word
此分上一篇:[UVA][dp] 11569 - Lovely Hint

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86