题意就是 将循环小数 化为 分数
代码如下,
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
char s[20],a[20],b[20];
int gcd(int x, int y)
{
int temp;
while(x%y)
{
temp = x%y;
x = y;
y = temp;
}
return y;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s)
int ls = strlen(s),i;
int x=0,y=0,fx=0,fy=0;
for(i = 2; i < ls; i++)
{
if(s=='(')
{
fy = fx;
y = x;
for(int j = i+1; j < ls; j++)
{
if(s[j]==')') break;
y *= 10;
y += s[j] - '0';
fy ++;
}
break;
}
x *= 10;
x += s-'0';
fx++;
}
if(fy==0)
{
int fm = 1;
while(fx--)
fm *= 10;
int tf = gcd(x,fm);
printf("%d/%d\n",x/tf,fm/tf);
}
else
{
int px=1,py=1;
while(fx--)
px *= 10;
while(fy--)
py *= 10;
int tp = py-px;
int ty = y - x;
int tf = gcd(ty,tp);
printf("%d/%d\n",ty/tf,tp/tf);
}
}
return 0;
}
|