Description
请将有限小数化为最简分数。
Input
一个整数n 表示需要转化的小数个数; 接下来n行,每行有一个有限小数。(保证小数位数不超过9位)
Output
输出有n行,每行为小数对应的最简分数
Sample Input
20.50.4
Sample Output
1/22/5
Hint
注意精度问题,数据保证不会有类似1.0的小数。
注意下精度, 浮点数在内存中是有误差的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const __int64 mm = 1000000000;
const double esp = 1e-11;
__int64 gcd(__int64 a, __int64 b){
if(b == 0)
return a;
return gcd(b,a%b);
}
int main(){
double a;
__int64 b;
int T;
scanf("%d",&T);
while(T--){
scanf("%lf",&a);
b = (a+esp)*mm;
__int64 temp = gcd(b,mm);
printf("%I64d/%I64d\n", b/temp,mm/temp);
}
return 0;
}