Skip to content

Commit be22458

Browse files
committed
Refactor SolveEquation
1 parent b0406c3 commit be22458

File tree

1 file changed

+42
-87
lines changed

1 file changed

+42
-87
lines changed

UVa/SolveEquation.java

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,54 @@
11
/**
2-
* Let us look at a boring mathematics problem. :-)
3-
* We have three different integers, x, y and z, which satisfy the following three relations:
4-
* • x + y + z = A
5-
* • xyz = B
6-
* • x
7-
* 2 + y
8-
* 2 + z
9-
* 2 = C
10-
* You are asked to write a program that solves for x, y and z for given values of A, B and C.
11-
* Input
12-
* The first line of the input file gives the number of test cases N (N < 20). Each of the following N lines
13-
* gives the values of A, B and C (1 ≤ A, B, C ≤ 10000).
14-
* Output
15-
* For each test case, output the corresponding values of x, y and z. If there are many possible answers,
16-
* choose the one with the least value of x. If there is a tie, output the one with the least value of y.
17-
* If there is no solution, output the line ‘No solution.’ instead.
18-
* Sample Input
19-
* 2
20-
* 1 2 3
21-
* 6 6 14
22-
* Sample Output
23-
* No solution.
24-
* 1 2 3
2+
* Let us look at a boring mathematics problem. :-) We have three different
3+
* integers, x, y and z, which satisfy the following three relations: • x + y +
4+
* z = A • xyz = B • x 2 + y 2 + z 2 = C You are asked to write a program that
5+
* solves for x, y and z for given values of A, B and C. Input The first line of
6+
* the input file gives the number of test cases N (N < 20). Each of the
7+
* following N lines gives the values of A, B and C (1 ≤ A, B, C ≤ 10000).
8+
* Output For each test case, output the corresponding values of x, y and z. If
9+
* there are many possible answers, choose the one with the least value of x. If
10+
* there is a tie, output the one with the least value of y. If there is no
11+
* solution, output the line ‘No solution.’ instead. Sample Input 2 1 2 3 6 6 14
12+
* Sample Output No solution. 1 2 3
2513
*/
26-
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2612
2714

28-
import static java.lang.Integer.parseInt;
29-
import static java.lang.System.exit;
15+
// https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2612
3016

31-
import java.io.BufferedReader;
32-
import java.io.IOException;
33-
import java.io.InputStreamReader;
34-
import java.io.OutputStreamWriter;
35-
import java.io.PrintWriter;
36-
import java.util.StringTokenizer;
17+
import java.util.Scanner;
3718

3819
public class SolveEquation {
3920

40-
static void solve() throws Exception {
41-
42-
int numberOfTestCases = nextInt();
43-
while (numberOfTestCases != 0) {
44-
45-
int A = nextInt();
46-
int B = nextInt();
47-
int C = nextInt();
48-
boolean hasSolution = false;
49-
50-
for (int x = -22; x <= 22 && !hasSolution; x++) {
51-
if (x * x <= C) {
52-
for (int y = -100; y <= 100 && !hasSolution; y++) {
53-
if (x!= y && y * y <= C && (x * x + y * y <= C)) {
54-
int z = A - x - y;
55-
if ((z != y && z != x && x * x + y * y + z * z == C) && x * y * z == B) {
56-
hasSolution = true;
57-
System.out.println(x + " " + y + " " + z);
58-
}
21+
public static void main(String[] args) {
22+
Scanner input = new Scanner(System.in);
23+
24+
int numberOfTestCases = input.nextInt();
25+
while (numberOfTestCases != 0) {
26+
27+
int A = input.nextInt();
28+
int B = input.nextInt();
29+
int C = input.nextInt();
30+
boolean hasSolution = false;
31+
32+
for (int x = -22; x <= 22 && !hasSolution; x++) {
33+
if (x * x <= C) {
34+
for (int y = -100; y <= 100 && !hasSolution; y++) {
35+
if (x != y && y * y <= C && (x * x + y * y <= C)) {
36+
int z = A - x - y;
37+
if ((z != y && z != x && x * x + y * y + z * z == C)
38+
&& x * y * z == B) {
39+
hasSolution = true;
40+
System.out.println(x + " " + y + " " + z);
41+
}
42+
}
43+
}
44+
}
45+
}
46+
if (!hasSolution) {
47+
System.out.println("No solution.");
5948
}
60-
}
61-
}
62-
}
63-
if (!hasSolution) {
64-
System.out.println("No solution.");
65-
}
66-
67-
numberOfTestCases--;
68-
}
69-
70-
}
71-
72-
static int nextInt() throws IOException {
73-
return parseInt(next());
74-
}
7549

76-
static String next() throws IOException {
77-
while (tok == null || !tok.hasMoreTokens()) {
78-
tok = new StringTokenizer(in.readLine());
79-
}
80-
return tok.nextToken();
81-
}
50+
numberOfTestCases--;
51+
}
8252

83-
public static void main(String[] args) {
84-
try {
85-
in = new BufferedReader(new InputStreamReader(System.in));
86-
out = new PrintWriter(new OutputStreamWriter(System.out));
87-
solve();
88-
in.close();
89-
out.close();
90-
} catch (Throwable e) {
91-
e.printStackTrace();
92-
exit(0);
9353
}
94-
}
95-
96-
static BufferedReader in;
97-
static PrintWriter out;
98-
static StringTokenizer tok;
9954
}

0 commit comments

Comments
 (0)