模拟题,自下而上模拟分子和分母。
#include <cstdio>
int a[10];
int b[10];
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main(int argc, char const *argv[]) {
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
int fz = b[n], fm = a[n];
for (int i = n-1; i >= 1; i--) {
fz = a[i] * fm + fz;
int newfm = fz;
fz = b[i] * fm;
fm = newfm;
}
printf("Case #%d: %d %d\n", t, fz / gcd(fz, fm), fm / gcd(fz, fm));
}
return 0;
}