D - So Hard FZU - 2193


请将有限小数化为最简分数。
Input
一个整数n 表示需要转化的小数个数; 接下来n行,每行有一个有限小数。(保证小数位数不超过9位)
Output
输出有n行,每行为小数对应的最简分数
Sample Input
2
0.5
0.4
Sample Output
1/2
2/5
Hint

注意精度问题,数据保证不会有类似1.0的小数。


因为double精度不够wa到绝望。。。

#include<cstdio>
#include<cmath>
#include<cstring>
int main(){
    char s[100];
    int n;
    scanf("%d",&n);getchar();
    while(n--){
    	long long k,a=0,b=1,t=1,p=0;
        scanf("%s",&s);
        int len=strlen(s);
        for(int i=len-1;i>=0;i--){
        	if(s[i]=='.')b=t;
        	else if(s[i]=='-')p++;
			else{
				a+=(s[i]-'0')*t;t*=10;
			}
		}
        while(1){
        	if(b%2==0&&a%2==0){b/=2;a/=2;}
        	else break;
		}
		while(1){
        	if(b%5==0&&a%5==0){b/=5;a/=5;}
        	else break;
		}
        if(p)printf("-");
        printf("%I64d/%I64d\n",a,b);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值