/*
循环小数化分数:
小数形式:循环部分&循环部分 【&】表示连接
分数形式: (非循环部分&循环部分)/( (10^(循环节位数)-1)^(非循环节位数) )
例:
0.32(692307)
32692307 / 99999900 = 17/52
注意的输入:0.00(1)
*/
#include<stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#define PI 3.1415926
#define STOP system("pause")
using namespace::std;
int gcd(int a, int b)
{
while(b^=a^=b^=a%=b);
return a;
}
int bits(int x)
{
int ans = 0;
while(x)
{
ans ++;
x /= 10;
}
return ans;
}
int main()
{
int a, b, t, n, b1, b2, x, y, i;
char temp;
scanf("%d", &n);
while(n--)
{
a = b = 0;
scanf(" 0.");
i = 0;
while(temp = getchar(), temp >= '0' && temp <= '9')
{
a = a * 10 + (temp -'0');
i ++;
}
b1 = i;
if(temp == '\n')
{
y = 1;
for(i=0; i<b1; i++)
y *= 10;
x = a;
t = gcd(x, y);
printf("%d/%d\n", x/t, y/t);
continue;
}
i = 0;
while(temp = getchar(), temp >= '0' && temp <= '9')
{
b = b * 10 + temp - '0';
i++;
}
b2 = i;
x = a;
for(i=0; i<b2; i++)
x *= 10;
x += b;
x -= a;
y = 1;
for(i=0; i<b2; i++)
y *= 10;
y -= 1;
for(i=0; i<b1; i++)
y *= 10;
t = gcd(x, y);
printf("%d/%d\n", x / t, y / t);
}
return 0;
}
hdoj 1717 小数化分数2【水】
最新推荐文章于 2020-05-08 12:40:45 发布