Pagini recente » Cod sursa (job #134761) | Cod sursa (job #881633) | Cod sursa (job #281158) | Cod sursa (job #1438838) | Cod sursa (job #2909249)
#include <bits/stdc++.h>
using namespace std;
#ifndef DLOCAL
#define cin fin
#define cout fout
ifstream cin("gauss.in");
ofstream cout("gauss.out");
#endif
const double eps = 1e-9;
template<typename T>
struct Vector {
vector<T> v;
T get(int i) {return v[i]; }
Vector operator * (const T& x) const {
Vector rez;
rez.v.resize(v.size());
for(int i = 0; i < v.size(); i++)
rez.v[i] = v[i] * x;
return rez;
}
void operator *= (const T& x) {
*this = (*this) * x;
}
void operator += (const Vector& x) {
assert(v.size() == x.v.size());
for(int i = 0; i < v.size(); i++)
v[i] += x.v[i];
}
};
int main() {
vector<double> rez;
vector< Vector<double> > gauss;
int n, m;
cin >> n >> m;
rez.resize(m);
for(int i = 0; i < n; i++) {
gauss.resize(gauss.size() + 1);
gauss.back().v.resize(m + 1);
for(auto &x : gauss.back().v)
cin >> x;
}
while(gauss.size() < m) {
gauss.resize(gauss.size() + 1);
gauss.back().v.resize(m + 1);
for(auto &x : gauss.back().v)
x = 0;
}
n = gauss.size();
Vector<double> temp;
for(int i = 0; i < m; i++) {
int poz = i;
for(int j = i; j < n; j++)
if(abs(gauss[j].get(i)) >= eps)
poz = j;
// cerr << poz << ' ';
if(poz != i)
swap(gauss[i], gauss[poz]);
if(gauss[i].get(i) == 0) {
continue;
}
gauss[i] *= 1.0 / gauss[i].get(i);
for(int j = 0; j < n; j++)
if(j != i)
gauss[j] += gauss[i] * -gauss[j].get(i);
}
for(int i = 0; i < m; i++) {
rez[i] = gauss[i].get(m);
}
for(int i = m; i < n; i++)
if(gauss[i].get(m) != 0) {
cout << "Imposibil\n";
exit(0);
}
cout << setprecision(8) << fixed;
for(auto x : rez)
cout << x << ' ';
cout << '\n';
}
// 1 3 4
// x1 * 1 + x2 * 1 + x3 * -1 = 0
// x1 * 2 + x2 * 2 + x3 * -2 = 0
// x1 * 0 + x2 * 0 + x3 * 0 = 0