1. package timberman.ui;
2.
3. import java.util.Scanner;
4. public class homework {
5. public static void main(String[] args) {
6.
7. Scanner input =new Scanner(System.in);
8.
9. int []first_num=new int [2];
10. int []second_num=new int [2];
11.
12. System.out.println("请输入第一个数字的分子");
13. first_num[0]= input.nextInt();
14. System.out.println("请输入第一个数字的分母");
15. first_num[1]= input.nextInt();
16. System.out.println("请输入第二个数字的分子");
17. second_num[0]= input.nextInt();
18. System.out.println("请输入第二个数字的分母");
19. second_num[1]= input.nextInt();
20.
21. long temp_mother=(first_num[1]*second_num[1])/gcd(first_num[1],second_num[1]);
22. long temp_son=first_num[0]*(temp_mother/first_num[1])-second_num[0]*(temp_mother/second_num[1]);
23.
24. long real_gcd=gcd(temp_son,temp_mother);
25. long real_son=temp_son/real_gcd;
26. long real_mother=temp_mother/real_gcd;
27.
28. if(real_son%real_mother==0){
29. System.out.println(real_son/real_mother);
30. }
31. else{
32. System.out.println(real_son+"/"+real_mother);
33. }
34. }
35.
36. public static long gcd(long a,long b) {
37. while(b!=0){
38. long temp=a%b;
39. a=b;
40. b=temp;
41. }
42. return a;
43. }
44. }