首页 > 试题广场 >

Rational Sum (20)

[编程题]Rational Sum (20)
  • 热度指数:9027 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.

输入描述:
Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int".  If there is a negative number, then the sign must appear in front of the numerator.


输出描述:
For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor.  You must output only the fractional part if the integer part is 0.
示例1

输入

5
2/5 4/15 1/30 -2/60 8/3

输出

3 1/3

有一说一,牛客网的测试用例更严格

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (br.readLine() != null) {
            List<String> list = Arrays.asList(br.readLine().split(" "));
            List<Long> numeratorList = list.stream()
                .map(x -> Long.parseLong(x.split("/")[0]))
                .collect(Collectors.toList());
            List<Long> denominatorList = list.stream()
                .map(x -> Long.parseLong(x.split("/")[1]))
                .collect(Collectors.toList());
            if (list.size() < 2) {
                format(numeratorList.get(0), denominatorList.get(0));
                continue;
            }
            long denominator = lcm(denominatorList);
            long numerator = 0L;
            for (int i = 0; i < list.size(); ++i) {
                numerator += numeratorList.get(i) * (denominator / denominatorList.get(i));
            }
            format(numerator, denominator);
        }        
    }
    // 求最大公约数
    private static long gcd(long a, long b) {
        if (a < b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        long c;
        while ((c = (a % b)) != 0) {
            a = b;
            b = c;
        }
        return b;
    }
    // 求最小公倍数
    private static long lcm(long a, long b) {
        return (a * b) / gcd(a, b);
    }
    // 求一串数的最小公倍数
    private static long lcm(List<Long> list) {
        long start = lcm(list.get(0), list.get(1));
        for (int i = 2; i < list.size(); ++i) {
            start = lcm(start, list.get(i));
        }
        return start;
    }
    //分数简化,转换成结果,输出
    private static void format(long numerator, long denominator) {
        if (numerator == 0) { //整数和小数部分都为0的情况
            System.out.println(0);
            return;
        }
        long tmp = Math.abs(numerator);
        long a =  tmp / denominator;
        long res = tmp - a * denominator;
        if (res == 0) {
            System.out.print(numerator / denominator);
            return;
        }
        if (a != 0) {
            System.out.format("%d ", a);
        }
        long gcdNum = gcd(res, denominator);
        if (numerator < 0) {
            res = -res;
        }
        System.out.format("%d/%d\n", res / gcdNum, denominator / gcdNum);
    }
}
发表于 2025-01-09 23:54:31 回复(0)