求循环小数的分数形式

求循环小数的分数形式

Wed Oct 02 2024
zzcoe
1 分钟

知道一个循环小数求他的分数形式,p,q是循环区间

CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int p,q,i;
    int m,s,n;
    cin>>p>>q>>s;
    m=s-int(s/pow(10,q-p+1));
    n=(pow(10,q)-pow(10,p-1));
    cout<<m/__gcd(m,n)<<" "<<n/__gcd(m,n);
    return 0;
}
//例
//1 6 
//142857
//输出 1 7