With two unknowns, a system can be solved with one formula, called Cramer's rule, based on the determinant.
What to build
Solve a·x + b·y = e and c·x + d·y = f, or say there is no unique solution.
Requirements
- Read the two rows
a b eandc d f. - If a·d − b·c is 0, print
no unique solution. - Otherwise print x and y with 4 decimal places.
Input
Two lines, each with three whole numbers: a b e and c d f.
Output
Either no unique solution, or two lines x = X and y = Y to 4 decimal places.
Constraints
All numbers are between -100 and 100.
Example
Input:
2 1 5
1 -1 1
Output:
x = 2.0000
y = 1.0000
Why it works
The determinant a·d − b·c is the area scale of the matrix. When it is 0 the two equations describe parallel or identical lines. Otherwise the formulas give the single crossing point.
Examples
2 1 5 1 -1 1
x = 2.0000 y = 1.0000
1 2 3 2 4 6
no unique solution
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
For a·x + b·y = e and c·x + d·y = f, let det = a·d − b·c.
Hint 2
If det is 0 there is no single answer. Otherwise x = (e·d − b·f) ÷ det and y = (a·f − e·c) ÷ det.
Hint 3
Print with `round(v, 4) + 0.0` first, then `:.4f`.
Solution
Show one correct solution (try it yourself first)
a, b, e = [int(v) for v in input().split()]
c, d, f = [int(v) for v in input().split()]
det = a * d - b * c
if det == 0:
print("no unique solution")
else:
x = (e * d - b * f) / det
y = (a * f - e * c) / det
print(f"x = {round(x, 4) + 0.0:.4f}")
print(f"y = {round(y, 4) + 0.0:.4f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step