1 条题解
-
0
【本题解由站长编写】
解二元一次方程组 a·x + b·y = c,d·x + e·y = f。消元即可——D = a·e - b·d,x = (c·e - b·f)/D,y = (a·f - c·d)/D。参数范围不大但乘积可能溢出 int,用 long long 存参数和中间量;除法前先转 double 以免整数除法截断。
代码(C++17)
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; long long D = a * e - b * d; if (D == 0) { cout << "no solution" << endl; return 0; } double x = (double)(c * e - b * f) / D; double y = (double)(a * f - c * d) / D; if (x == (long long)x) cout << (long long)x << " "; else cout << x << " "; if (y == (long long)y) cout << (long long)y << endl; else cout << y << endl; return 0; }
- 1
信息
- ID
- 4215
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者