|
发表于 2022-10-29 19:44:10
|
显示全部楼层
- #include <stdio.h>
- typedef struct Equation {
- int a, b, c;
- }Equation;
- typedef struct Pair {
- float x, y;
- }Pair;
- /*
- | a b |
- | c d |
- */
- int delta(int a, int b, int c, int d) {
- return a * d - c * b;
- }
- Pair solution(Equation E1, Equation E2) {
- float x = delta(E1.c, E1.b, E2.c, E2.b) / delta(E1.a, E1.b, E2.a, E2.b);
- float y = delta(E1.a, E1.c, E2.a, E2.c) / delta(E1.a, E1.b, E2.a, E2.b);
- Pair pair = { x, y };
- return pair;
- }
- int main(void) {
- Equation
- E1 = { 1, -1, 5 }, // x - y = 5
- E2 = { 2, 1, 4 }; // 2x + y = 4
- Pair ans = solution(E1, E2); // x = 3, y = -2
- printf("x = %.2f, y = %.2f\n", ans.x, ans.y);
- return 0;
- }
复制代码 |
|