⬆︎
×

[PAT-A] 1081 Rational Sum

Hyplus目录

Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] fractions = br.readLine().split(" ");
        long numerator = 0, denominator = 1;

        for (String fraction : fractions) {
            String[] parts = fraction.split("/");
            long a = Long.parseLong(parts[0]);
            long b = Long.parseLong(parts[1]);

            long lcm = (denominator * b) / gcd(denominator, b);
            numerator = numerator * (lcm / denominator) + a * (lcm / b);
            denominator = lcm;

            long commonDivisor = gcd(Math.abs(numerator), denominator);
            numerator /= commonDivisor;
            denominator /= commonDivisor;
        }

        if (numerator == 0) {
            System.out.println("0");
        } else if (denominator == 1) {
            System.out.println(numerator);
        } else {
            long integerPart = numerator / denominator;
            numerator %= denominator;
            if (integerPart != 0) {
                System.out.print(integerPart + " ");
            }
            System.out.println(numerator + "/" + denominator);
        }

        br.close();
    }
}

C++

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
    LL a = 0, b = 1;

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        LL c, d;
        scanf("%lld/%lld", &c, &d);

        LL t = gcd(c, d);
        c /= t;
        d /= t;

        t = gcd(b, d);
        a = d / t * a + b / t * c;
        b = b / t * d;

        t = gcd(a, b);
        a /= t;
        b /= t;
    }

    if (b == 1) cout << a;
    else {
        if (a >= b) {
            printf("%lld ", a / b);
            a %= b;
        }
        printf("%lld/%lld", a, b);
    }

    return 0;
}

发表评论