编程题:35
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.nextLine();
String[] fractions = scanner.nextLine().split(" ");

    int sumNumerator = 0;
    int sumDenominator = 1;

    for (String fraction : fractions) {
        String[] parts = fraction.split("/");
        int a = Integer.parseInt(parts[0]);
        int b = Integer.parseInt(parts[1]);

        sumNumerator = sumNumerator * b + a * sumDenominator;
        sumDenominator *= b;

        int gcd = gcd(sumNumerator, sumDenominator);
        sumNumerator /= gcd;
        sumDenominator /= gcd;
    }

    int avgNumerator = sumNumerator;
    int avgDenominator = sumDenominator * N;

    int gcd = gcd(avgNumerator, avgDenominator);
    avgNumerator /= gcd;
    avgDenominator /= gcd;

    if (avgDenominator == 1) {
        System.out.println(avgNumerator);
    } else {
        System.out.println(avgNumerator + "/" + avgDenominator);
    }
}

public static int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

}

posted on 2025-05-24 23:47  Swishy  阅读(7)  评论(0)    收藏  举报