第2题 循环小数 提交文件:float.pas/.cpp 输入文件:float.in 输出文件:float.out
给出一个循环小数X,X = 0.a1a2…an(b1b2…bm)。 (b1b2…bm)代表小数的循环节。例如:0.5 = 0.50 = 0.5(0) = 0.5(00) = 1/2, 0.3(3) = 0.333(33) = 1/3。现在,你需要将这个循环小数转化为分数形式A/B(A和B的公约数必须为1)
输入格式: 第一行有两个正整数n, m。 第二行有n个正整数a1a2…an。 第三行有m个正整数b1b2…bm。 输出格式: 输出两个正整数A和B,用空格隔开,表示分数A/B。
输入样例: 4 2 3333 33
输出样例: 1 3 数据范围: 对于20%数据 1 ≤ n, m ≤ 6 对于所有数据 1 ≤ n, m ≤ 8
题解:
#include<cstdio> #include<cmath> using namespace std; int n,m; __int64 a,b,z,x,y; __int64 solve(__int64 a,__int64 b) { return b==0?a:solve(b,a%b); } int main() { freopen("float.in","r",stdin); freopen("float.out","w",stdout); scanf("%d%d%I64d%I64d",&n,&m,&a,&b); x=a; y=0; while(m--){ x*=10; y=y*10+9; } x=x+b-a; while(n--){ y*=10; } z=solve(x,y); printf("%I64d %I64d\n",x/z,y/z); return 0; }