Submission

Status:

PP------PP

Subtask/Task Score:

40/100

Score: 40

User: I_am_me

Problemset: Fraction

Language: c

Time: 0.001 second

Submitted On: 2025-10-12 16:54:38

#include <stdio.h>

int gcd(int a, int b) {
    if (a < 0) a = -a;
    if (b < 0) b = -b;
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int first[2];
    for (int i = 0; i < 2; i++) scanf("%d", &first[i]);

    int numoper;
    scanf("%d", &numoper);

    char oper[numoper];
    int num_to_oper[numoper][2];

    for (int i = 0; i < numoper; i++) {
        scanf(" %c", &oper[i]);
        scanf("%d %d", &num_to_oper[i][0], &num_to_oper[i][1]);
    }

    int ans[2];

    for (int i = 0; i < numoper; i++) {
        if (i >= 1) {
            first[0] = ans[0];
            first[1] = ans[1];
        }

        int a = first[0], b = first[1];
        int c = num_to_oper[i][0], d = num_to_oper[i][1];

        if (oper[i] == '+') {
            ans[0] = a * d + b * c;
            ans[1] = b * d;
        }
        else if (oper[i] == '-') {
            ans[0] = a * d - b * c;
            ans[1] = b * d;
        }
        else if (oper[i] == '*') {
            ans[0] = a * c;
            ans[1] = b * d;
        }
        else if (oper[i] == '/') {
            ans[0] = a * d;
            ans[1] = b * c;
        }

        if (ans[1] < 0) {
            ans[0] = -ans[0];
            ans[1] = -ans[1];
        }

        if (ans[0] == 0) {
            ans[1] = 1;
        } else {
            int g = gcd(ans[0], ans[1]);
            ans[0] /= g;
            ans[1] /= g;
        }

        printf("%d %d\n", ans[0], ans[1]);
    }

    return 0;
}