首页 > 代码库 > HDU 1717 小数化分数2

HDU 1717 小数化分数2

http://acm.hdu.edu.cn/showproblem.php?pid=1717

 

之前莫名WA了挺多次的有趣的题orz 感觉还是写麻烦了

注意字符串处理

 

代码:  0MS  1068K

#include <cstdio>#include <cstring>using namespace std;int gcd(int m, int n) {    int r;    while (n) {        r = m % n;        m = n;        n = r;    }    return m;}int main() {    int t;    char s[15];    scanf("%d", &t);    while (t--) {        scanf("%s", s);        int a = 0, b = 0, a10 = 1, b10 = 1, i = 2, lens = strlen(s) - 2;        while (s[i] != ( && i < lens + 2) {            a = a * 10 + s[i++] - 0;            a10 *= 10;        }        int pos = i++;        while (s[i] != ) && i < lens + 2) {            b = b * 10 + s[i++] - 0;            b10 *= 10;        }        if (!a && !b) {            printf("0\n");        }        else {            int m, n, lenend = strlen(s + pos);            if (*(s + pos) == () {                lens -= 2;                lenend -= 2;            }            if (!b) {                m = a;                n = a10;            }            else {                m = a * b10 + b - a;                n = (b10 - 1) * a10;            }            int gcdn = gcd(n, m);            printf("%d/%d\n", m / gcdn, n / gcdn);        }    }    return 0;}

 

HDU 1717 小数化分数2