60分啊
查看原帖
60分啊
749028
QAQ5楼主2025/1/2 11:44
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Fraction one = new Fraction(scanner.nextLine()), two = new Fraction(scanner.nextLine());
        Fraction result = mulFraction(one, two);
        System.out.println(result.month + " " + result.son);
    }

    //计算分数结果化简
    static Fraction mulFraction(Fraction one, Fraction two) {
        int mulSon = one.son * two.son, mulMonth = one.month * two.month;
        //除到直到不能被2整除
        while (mulSon % 2 == 0 && mulMonth % 2 == 0) {
            mulSon /= 2;
            mulMonth /= 2;
        }
        //母除子的整除情况不考虑
        if (mulMonth % mulSon == 0) {
            int cache = mulSon;
            mulSon = 1;
            mulMonth /= cache;
        }
        return new Fraction(mulSon, mulMonth);
    }
}

class Fraction {
    int son, month;

    public Fraction(int son, int month) {
        this.son = son;
        this.month = month;
    }

    public Fraction(String fraction) {
        String[] two = fraction.split("/");
        String son = two[0], month = two[1];
        this.son = Integer.parseInt(son);
        this.month = Integer.parseInt(month);
    }

    @Override
    public String toString() {
        return month + "/" + son;
    }
}

2025/1/2 11:44
加载中...