先贴代码
#include <bits/stdc++.h>
//#define debug
using namespace std;
namespace mihomo
{
inline const bool
equal(const double& __x,
const double& __y)
{ return fabs(__x-__y) < 1e-9; }
class gaussian
{
private:
int _row;
int _col;
double** _LHS;
double* _RHS;
int _statue;
public:
gaussian(int __row, int __col)
: _row(__row), _col(__col)
{
_LHS = new double* [_row];
_RHS = new double [_row] ();
int __i, __j;
for (__i = 0; __i < _row; __i++)
_LHS[__i] = new double [_col] ();
_statue = 0;
}
~gaussian(void)
{
int __i;
for (__i = 0; __i < _row; __i++)
delete [] (_LHS[__i]);
delete [] (_LHS);
delete [] (_RHS);
}
inline double*
operator[](int __pos)
{ return _LHS[__pos]; }
inline double&
operator()(int __pos)
{ return _RHS[__pos]; }
friend inline ostream&
operator<<( ostream& os,
gaussian& __x )
{
int __i, __j;
os << "--------------------------------\n";
for (__i = 0; __i < __x._row; __i++)
{
for (__j = 0; __j < __x._col; __j++)
os << __x[__i][__j] << ' ';
os << "|" << __x._RHS[__i] << endl;
}
os << "--------------------------------\n";
}
inline const int&
solve(void)
{
int __i, __j, __k, __mx, __nw;
double __coef;
for (__k = 0; __k < _row && __k < _col; __k++)
{
#ifdef debug
cout << (*this);
#endif
__mx = __nw;
for (__i = __nw + 1; __i < _row; __i++)
if (abs(_LHS[__i][__k]) > abs(_LHS[__mx][__k]))
__mx = __i;
if (equal(_LHS[__mx][__k], 0)) continue;
if (__mx != __nw)
swap(_LHS[__mx], _LHS[__nw]),
swap(_RHS[__mx], _RHS[__nw]);
#ifdef debug
cout << (*this);
#endif
__coef = _LHS[__nw][__k];
for (__j = __k; __j < _col; __j++)
_LHS[__nw][__j] /= __coef;
_RHS[__nw] /= __coef;
#ifdef debug
cout << (*this);
#endif
for (__i = 0; __i < _row; __i++)
{
if (__i == __nw) continue;
__coef = _LHS[__i][__k];
for (__j = __k; __j < _col; __j++)
_LHS[__i][__j] -= __coef * _LHS[__nw][__j];
_RHS[__i] -= __coef * _RHS[__nw];
}
__nw++;
#ifdef debug
cout << (*this);
#endif
}
if (__nw == _row) return _statue = 1;
while (__nw < _row)
if (!equal(_RHS[__nw++], 0)) return _statue = -1;
return _statue = INT_MAX;
}
inline const int&
statue(void) const
{ return _statue; }
};
};
using namespace mihomo;
int main(void)
{
int n, x;
cin >> n;
gaussian equations(n, n);
for (int i(0); i < n; i++)
{
for (int j(0); j < n; j++)
{
cin >> x;
equations[i][j] = (double)x;
}
cin >> x;
equations(i) = (double)x;
}
if (equations.solve() == 1)
for (int i(0); i < n; i ++)
cout << fixed << setprecision(2) << 'x' << i+1 << '=' << equations(i) << endl;
else if (equations.statue() == -1) cout << -1;
else cout << 0;
return 0;
}
此代码在c++14(GCC 9)(不开O2)下AC记录详情
在C++14开启O2下40pts记录详情
在c++11\c++14不开启O2下0ptsRE记录详情 记录详情
在c++14(GCC 9)开启O2下0ptsRE记录详情
概况如下
| 关闭O2 | 开启O2 | |
|---|---|---|
| C++14(GCC 9) | AC | 0pts(RE) |
| C++14 | 0pts(RE) | 40pts |
| C++11 | 0pts(RE) | NONE |
以上RE皆为Segmentation fault with invalid memory reference.(即访问非法内存错误) 保证代码与贴中代码相同 有没有神犇帮本蒟蒻解释一下(违规紫砂)