Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cmath>
- #include <iomanip>
- #include <vector>
- using namespace std;
- long double EPS = 0.0000000000001;
- ifstream cin("linear.in");
- ofstream cout("linear.out");
- int main() {
- cin.sync_with_stdio(false);
- cout.sync_with_stdio(false);
- int n;
- cin >> n;
- vector< vector<long double> > vv(n, vector<long double>(n + 1));
- vector<int> where(n, -1);
- vector<long double> ans(n, 0.0);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n + 1; j++)
- cin >> vv[i][j];
- int row = 0;
- for (int col = 0; col < n && row < n; col++) {
- int cur = row;
- for (int i = cur; i < n; i++)
- if (abs(vv[i][col]) > abs(vv[cur][col]))
- cur = i;
- if (abs(vv[cur][col]) < EPS)
- continue;
- for (int i = col; i < n + 1; i++)
- swap(vv[cur][i], vv[row][i]);
- long double d = vv[row][col];
- for (int i = col; i < n + 1; i++)
- vv[row][i] /= d;
- where[col] = row;
- for (int i = row + 1; i < n; i++) {
- long double c = vv[i][col];
- for (int j = col; j < n + 1; j++)
- vv[i][j] -= vv[row][j] * c;
- }
- row++;
- }
- for(int i = row; i < n; i++)
- if (abs(vv[i][n]) > EPS) {
- cout << "impossible";
- return 0;
- }
- for (int i = 0; i < n; i++)
- if (where[i] == -1) {
- cout << "infinity";
- return 0;
- }
- for (int i = n - 1; i > -1; i--) {
- long double c = vv[i][n];
- for (int j = n - 1; j > i; j--)
- c -= ans[j] * vv[i][j];
- ans[i] = c / vv[i][i];
- }
- cout << "single" << endl;
- for (int i = 0; i < n; i++)
- cout << fixed << setprecision(3) << ans[i] << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement