编程题:33
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] parts = input.split(" ");
String[] part1 = parts[0].split("/");
String[] part2 = parts[1].split("/");
int a1 = Integer.parseInt(part1[0]);
int b1 = Integer.parseInt(part1[1]);
int a2 = Integer.parseInt(part2[0]);
int b2 = Integer.parseInt(part2[1]);
int numerator = a1 * b2 + a2 * b1;
int denominator = b1 * b2;
int gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
if (denominator == 1) {
System.out.println(numerator);
} else {
System.out.println(numerator + "/" + denominator);
}
}
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
浙公网安备 33010602011771号