Pagini recente » Cod sursa (job #2410906) | Cod sursa (job #747859) | Cod sursa (job #1482203) | Cod sursa (job #198491) | Cod sursa (job #1471059)
#include <vector>
#include <algorithm>
#include <utility>
#include <fstream>
#include <functional>
#include <iomanip>
using namespace std;
using namespace std::placeholders;
constexpr bool e_zero(const double d){
return -1e-7 <= d && d <= 1e-7; }
int main(){
ifstream f("gauss.in");
ofstream g("gauss.out");
int n, m;
f >> n >> m;
vector<vector<double> > sist(n, vector<double>(m+1));
for(auto& x : sist){
for(auto& y : x){
f >> y; } }
for(int rand_cur = 0, var_de_rez = 0;
rand_cur < n && var_de_rez < m;
++rand_cur, ++var_de_rez){
{
int rand_real = rand_cur;
while(rand_real < n && e_zero(sist[rand_real][var_de_rez])){
++rand_real; }
if(rand_real >= n){
continue; }
swap(sist[rand_cur], sist[rand_real]); }
const double d = sist[rand_cur][var_de_rez];
transform(begin(sist[rand_cur])+var_de_rez, end(sist[rand_cur]),
begin(sist[rand_cur])+var_de_rez,
bind(divides<double>(), _1, d));
for(int rand_sec = 0; rand_sec < n; ++rand_sec){
if(rand_sec != rand_cur){
const double factor = sist[rand_sec][var_de_rez];
transform(begin(sist[rand_sec])+var_de_rez, end(sist[rand_sec]),
begin(sist[rand_cur])+var_de_rez,
begin(sist[rand_sec])+var_de_rez,
[factor](const double a, const double b){
return a - b * factor; }); } } }
if(any_of(begin(sist), end(sist), [m](const vector<double>& v){
return all_of(begin(v), end(v)-1, e_zero) &&
!e_zero(v[m]); })){
g << "Imposibil";
return 0; }
vector<double> rez(m, 0);
int j = 0;
for(int i = 0; i < n; ++i){
for( ; j < m && e_zero(sist[i][j]); ++j);
if(j < m){
rez[j] = sist[i][m]; } }
g << fixed << setprecision(10);
for(const auto x : rez){
g << x << '\n'; }
g << flush;
return 0; }