Pagini recente » Cod sursa (job #1906576) | Cod sursa (job #2048360) | Cod sursa (job #1543607) | Cod sursa (job #986845) | Cod sursa (job #2632974)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gauss.in");
ofstream fout("gauss.out");
const int NMAX = 305;
double a[NMAX][NMAX],rasp[NMAX];
int n,m;
bool dif_zero(double x){
if(x<-0.000000001 or x>0.000000001) 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 >> a[i][j];
for(int i=1,j=1;i<=n and j<=m;i++,j++){
for(int t=i;t<=n;t++){
if(dif_zero(a[t][j])==true){
for(int k=1;k<=m+1;k++)
swap(a[t][k],a[i][k]);
break;
}
}
if(dif_zero(a[i][j])==false){
i--;
continue;
}
for(int t=j+1;t<=m+1;t++) a[i][t]/=a[i][j];
a[i][j]=1;
for(int t=i+1;t<=n;t++){
for(int k=j+1;k<=m+1;k++)
a[t][k]-=a[i][k]*a[t][j];
a[t][j]=0;
}
}
for(int i=n;i>=1;i--){
for(int j=1;j<=m+1;j++){
if(dif_zero(a[i][j])==true){
if(j==m+1){
fout << "Imposibil";
return 0;
}
rasp[j]=a[i][m+1];
for(int t=j+1;t<=m;t++)
rasp[j]-=rasp[t]*a[i][t];
break;
}
}
}
fout << fixed << setprecision(8);
for(int i=1;i<=m;i++) fout << rasp[i] << ' ';
return 0;
}