Submission
Status:
PP------PP
Subtask/Task Score:
40/100
Score: 40
User: tha_smith
Problemset: Fraction
Language: cpp
Time: 0.004 second
Submitted On: 2025-10-09 22:34:46
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a,b,n;
cin >> a >> b >> n;
for(int i=0; i<n; i++) {
char c; int p,q,d;
cin >> c >> p >> q;
if(c=='+') {
a = a*q + b*p;
b *= q;
}
else if(c=='-') {
a = a*q - b*p;
b *= q;
}
else if(c=='*') {
a*=p;
b*=q;
}
else if(c=='/') {
a*=q;
b*=p;
}
//cout << a << ' ' << b << endl;
if(a==0) {
a=0;
b=1;
}
else {
d = __gcd(a,b);
a/=d;
b/=d;
//cout << d << ' ' << a << ' ' << b << endl;
if(b<0) { //if d returns negative integer -> return to previous + or -
a*=-1;
b*=-1;
}
}
cout << a << ' ' << b << endl;
}
}