Pagini recente » Cod sursa (job #1585770) | Cod sursa (job #1348068) | Cod sursa (job #245896) | Cod sursa (job #1853852) | Cod sursa (job #3253609)
//https://infoarena.ro/problema/gauss
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("fast-math")
//#pragma GCC optimize ("unroll-loops")
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
double mat[305][305];
int main()
{
ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n, m, i, j;
vector <int> rez;
fin >> n >> m;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m + 1; ++j)
{
fin >> mat[i][j];
//cout << mat[i][j] << " ";
}
//cout << "\n";
}
int c, r;
double rp;
for (r = 1, c = 1; ((r <= n) && (c <= m)); ++r, ++c)
{
//cout << r << " " << c << "\n";
if ((((-1e-8) <= mat[r][c]) && (((1e-8) >= mat[r][c]))))
{
for (i = r + 1; i <= n; ++i)
{
if ((((-1e-8) <= mat[i][c]) && (((1e-8) >= mat[i][c]))))
{
break;
}
}
if (i > n)
{
rez.push_back(0);
continue;
}
else
{
//cout << "facut";
swap(mat[r], mat[i]);
}
}
for (i = 1; i <= n; ++i)
{
if (i != r)
{
rp = mat[i][c] / mat[r][c];
//cout << i << " " << c << " " << r << "\n";
//cout << mat[i][c] << " " << mat[r][c] << " " << "\n";
for (j = 1; j <= m + 1; ++j)
{
mat[i][j] = mat[i][j] - rp * mat[r][j];
//cout << mat[i][j] << " ";
}
//cout << "\n";
}
}
//cout << "\n";
rez.push_back(r);
//cout << "++rez " << r+1 << "\n";
//cout << r+1 << " " << c+1 << "\n\n";
}
fout << setprecision(10) << fixed;
i = 1;
for (int x : rez)
{
fout << (x ? (mat[x][m + 1] / mat[x][i]) : 0.0000000000) << " ";
//cout << mat[x][m + 1] << " " << mat[x][i] << "\n";
++i;
}
return 0;
}