Pagini recente » Cod sursa (job #1721494) | Cod sursa (job #2250908) | Cod sursa (job #817861) | Cod sursa (job #977270) | Cod sursa (job #2589109)
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
const int NMAX = 300;
const long double EPS = 1e-8;
int N, M;
long double sys[NMAX + 5][NMAX + 5];
long double vals[NMAX + 5];
bool NotNull(long double X)
{
if(X < -EPS || X > EPS)
return true;
return false;
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M + 1; j++)
fin >> sys[i][j];
int l = 1, c = 1;
while(l <= N && c <= M)
{
int k;
for(k = l; k <= N; k++)
if(NotNull(sys[k][c]))
break;
if(k == N + 1) ///variabila libera, ii dam valoarea 0
{
c++;
continue;
}
///swap la liniile l, k
for(int j = 1; j <= M + 1; j++)
swap(sys[l][j], sys[k][j]);
for(int j = c + 1; j <= M + 1; j++)
sys[l][j] /= sys[l][c];
sys[l][c] = (long double)1;
for(int i = l + 1; i <= N; i++)
{
for(int j = c + 1; j <= M + 1; j++)
sys[i][j] -= sys[i][c] * sys[l][j];
sys[i][c] = 0;
}
l++, c++;
}
for(int i = N; i >= 1; i--)
for(int j = 1; j <= M + 1; j++)
if(NotNull(sys[i][j]))
{
if(j == M + 1)
{
fout << "Imposibil\n";
return 0;
}
vals[j] = sys[i][M + 1];
for(int c = j + 1; c <= M; c++)
vals[j] -= sys[i][c] * vals[c];
break;
}
for(int i = 1; i <= M; i++)
fout << fixed << setprecision(10) << vals[i] << ' ';
return 0;
}