循环小数

Problem Description
Given a floating point number X, the form is X=0.a1a2...an(b1b2...bm)(1<=n,m<=8). (b1b2...bm) indicates the repetend. For example, 0.5=0.50=0.5(0)=0.5(00)=1/2,0.3(3)=0.333(33)=1/3.
Please change the floating point number X into a fraction A/B(gcd(A,B)=1).
Input
Input may contain several test cases. The first line is a positive integer, T(T<=10) indicating the number of test cases below.
For each test case, the first line contains two integers n,m(1<=n,m<=8).
The following line contains n numbers a1a2...an.
The following line contains m numbers b1b2...bm.
Output
For each case, output the number A and B(0<A,B) on a single line.
Sample Input
2
2 2
50
00
4 2
3333
33
Sample Output
1 2
1 3
/*
题解:一个混循环小数的小数部分可以化成分数,这个分数的分子是第二个循环节以前的小数部分组成的数与小数部分中不循环部分组成的数的差。
分母的头几位数是9,末几位是0。9的个数与循环节中的位数相同,0的个数与不循环部分的位数相同。
*/
//标程:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
__int64  gcd(__int64 a,__int64 b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
//    freopen("a.txt","r",stdin);
    int t,  i;
    __int64 n, m, a, b;
    cin >> t;
    while(t --)
    {
        scanf("%I64d%I64d",&n,&m);     
        scanf("%I64d%I64d",&a,&b);
        __int64  tmpx = a;
        for(i = 1; i <= m; ++ i) tmpx *= 10;
        tmpx = tmpx + b - a;
        __int64 tmpy = 0;
        for(i = 0; i < m; ++ i) tmpy = tmpy*10+9;
        for(i = 1;i <= n; ++ i)  tmpy = tmpy*10;
        __int64 x = gcd(tmpx,tmpy);
        printf("%I64d %I64d\n",tmpx/x,tmpy/x);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值