hdu 1905 小数化分数2

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<stdlib.h>
#define ll __int64
#define pi acos(-1.0)
using namespace std;
int
gcd(int a,int b)
{

    int
t;
    if
(a<b) swap(a,b);
    while
(b)
    {

        t=a;
        a=b;
        b=t%b;
    }

    return
a;
}

int
pows(int n)
{

    int
ans=1;
    while
(n--)
        ans*=10;
    return
ans;
}

int
main()
{

    int
i,j,n,sum,t,pos1,pos2,pos3,a,b,c,len;
    string an;
    cin>>t;
    while
(t--)
    {

        cin>>an;
        len=an.length();
        pos2=an.find('(',0);
        pos3=an.find(')',pos2);
        if
(pos2<0)
        {

            a=0;
            for
(i=2;i<len;i++)
                a=a*10+an[i]-'0';
            b=pows(len-2);
        }

        else

        {

            a=0;
            for
(i=2;i<pos2;i++)
                a=a*10+an[i]-'0';
            c=0;
            for
(i=pos2+1;i<pos3;i++)
                c=c*10+an[i]-'0';
            b=pows(pos3-pos2-1);
            sum=a*b+c;
            a=sum-a;
            b=(b-1)*pows(pos2-2);
        }

        c=gcd(a,b);
        cout<<a/c<<'/'<<b/c<<endl;
    }

    return
0;
}

你可能感兴趣的:(HDU)