Skip to content

Commit 1938363

Browse files
committed
🎉 feat: initial commit for 1034 using java
1 parent 8185d04 commit 1938363

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
String[] input = br.readLine().split("[\\s/]");
10+
long a1 = Integer.parseInt(input[0]);
11+
long b1 = Integer.parseInt(input[1]);
12+
long a2 = Integer.parseInt(input[2]);
13+
long b2 = Integer.parseInt(input[3]);
14+
15+
if (b1 != 0 && b2 != 0) {
16+
add(a1, b1, a2, b2);
17+
minus(a1, b1, a2, b2);
18+
mutilply(a1, b1, a2, b2);
19+
divide(a1, b1, a2, b2);
20+
}
21+
}
22+
23+
public static void tackle(long a, long b) {
24+
if (a == 0) {
25+
System.out.print(0);
26+
return;
27+
}
28+
29+
boolean isMinus = a > 0 ? false : true;
30+
if (isMinus) {
31+
System.out.print("(-");
32+
a = -a;
33+
}
34+
35+
long gcd = getGcd(a, b);
36+
a = a / gcd;
37+
b = b / gcd;
38+
if (a % b == 0) {
39+
System.out.print(a / b);
40+
} else if (Math.abs(a) > b) {
41+
System.out.print(a / b + " " + (a % b) % b + "/" + b);
42+
} else if (a == b) {
43+
System.out.print(1);
44+
} else {
45+
System.out.print(a + "/" + b);
46+
}
47+
48+
if (isMinus) {
49+
System.out.print(")");
50+
}
51+
52+
}
53+
54+
public static void divide(long a1, long b1, long a2, long b2) {
55+
tackle(a1, b1);
56+
System.out.print(" / ");
57+
tackle(a2, b2);
58+
System.out.print(" = ");
59+
if (a2 == 0) {
60+
System.out.print("Inf");
61+
} else if (a2 < 0) {
62+
tackle(-1 * a1 * b2, -1 * a2 * b1);
63+
} else {
64+
tackle(a1 * b2, a2 * b1);
65+
}
66+
}
67+
68+
public static void mutilply(long a1, long b1, long a2, long b2) {
69+
tackle(a1, b1);
70+
System.out.print(" * ");
71+
tackle(a2, b2);
72+
System.out.print(" = ");
73+
tackle(a1 * a2, b1 * b2);
74+
System.out.println();
75+
}
76+
77+
public static void minus(long a1, long b1, long a2, long b2) {
78+
tackle(a1, b1);
79+
System.out.print(" - ");
80+
tackle(a2, b2);
81+
System.out.print(" = ");
82+
tackle(a1 * b2 - a2 * b1, b1 * b2);
83+
System.out.println();
84+
}
85+
86+
public static void add(long a1, long b1, long a2, long b2) {
87+
tackle(a1, b1);
88+
System.out.print(" + ");
89+
tackle(a2, b2);
90+
System.out.print(" = ");
91+
tackle(a1 * b2 + a2 * b1, b1 * b2);
92+
System.out.println();
93+
}
94+
95+
public static long getGcd(long a, long b) {
96+
while (a % b != 0) {
97+
long temp = a % b;
98+
a = b;
99+
b = temp;
100+
}
101+
return b;
102+
}
103+
}
104+

0 commit comments

Comments
 (0)