题解 P3389 【【模板】高斯消元法】

看到大家都没有重载运算符,那我就重载一下运算符给大家娱乐一下

我使用的是高斯-约旦消元法,这种方法是精度最高的(相对地)

一句话解释高斯约旦消元法:

通过加减消元法,依次制定x0,并通过加减消元法消去其他方程的x0的系数。对于这样的系数矩阵我们只进行初等变幻保证了其正确性

看代码吧,主要是希望帮助大家可以学到一些重载的方法

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int qr(void){
char c=getchar();
int x=0,q=1;
while(c<48||c>57)
q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)
x=(x<<1)+(x<<3)+(c^48),c=getchar();
return q*x;
}
#define RP(t,a,b) for(int t=(a),edd=(b);t<=edd;t++)
typedef double db;
const double EPS=1e-20;//这是坑点,一点要小一点,这个EPS。
int n;
const int maxn=105;
int ans[maxn];
inline db fabs(double x){
return x>=0?x:-x;
}
struct node{
db dat[maxn];
double& operator [](const int &x){
return dat[x];//重载运算符,返回引用 , 就算有dat有更多维,这样就好了,原理有关c++的“地址”系统
}
node operator *(const db &x){
node ans=(*this);//this是个指针,指向运算符左边的地址
for(int t=1;t<=n+1;t++)
ans[t]*=x;
return ans;
}
node operator /(const db &x){
node ans=(*this);
for(int t=1;t<=n+1;t++)
ans[t]/=x;
return ans;
}
node operator -(node &x){
node ans=(*this);
for(int t=1;t<=n+1;t++)
ans[t]-=x[t];
return ans;
}
node operator *=(const db &x){
return (*this)=(*this)*x;
}
node operator /=(const db &x){
return (*this)=(*this)/x;
}
node operator -=( node &x){
return (*this)=(*this)-x;
}
}data[maxn];
bool vis[maxn];
inline int big(int x){
db ans=0;
int ret;
for(int t=1;t<=n;t++)
if(!vis[t]&&ans<fabs(data[t][x]))
ret=t;
vis[ret]=1;//根据数学原理,不可重复选择一个方程来消元
return ret;//为了避免乘一个过小的数字,选择一个对于该未知数绝对值最大的系数
}
inline void kkk(void){
RP(t0,1,n){
int sttd=big(t0);
const db a=data[sttd][t0];
RP(t,1,n)
if(t!=sttd){
if(fabs(data[t][t0])<EPS){
cout<<"No Solution";//防止除0
return;
}
data[t]*=(a/data[t][t0]),data[t]-=data[sttd];//将选定x0的系数和基准方程变为一致,在通过加减消元消掉,
//此后该未知数的系数就是0,不会再产生影响
}
ans[t0]=sttd;//记录结果是哪个方程得出的
}
RP(t,1,n)
if(fabs(data[ans[t]][t])<EPS){
cout<<"No Solution"<<endl;
return;
}
RP(t,1,n){
printf("%.2lf\n",(data[ans[t]][n+1]/data[ans[t]][t]));
}
return;
}
int main(){
n=qr();
RP(t,1,n)
RP(i,1,n+1)
data[t][i]=qr();
kkk();
return 0;//功德圆满
}

最新文章

  1. STM32之通用定时器
  2. ng-controller event data
  3. dwz 在dialog里打开dialog
  4. SQL常用命令整理
  5. 初始jQuery
  6. jQuery动态实现title的修改 失效问题
  7. 浅谈html5某些新元素的用途
  8. H - Highways - poj 1751(prim)
  9. OpenGL ES 2.0 变换
  10. think in coding
  11. 全国各省市GeoCoord SQL文件(不包括区县)
  12. 对接第三方平台JAVA接口问题推送和解决
  13. C# 实现邮件发送
  14. c#事件的应用
  15. tp5 日志管理
  16. SNF软件开发机器人-子系统-功能-功能类型(普通表改为树型表)
  17. 在使用NSArray打印的时候如果遇到中文字符那么会打印出来编码。
  18. [Day2]变量、数据类型转换以及运算符
  19. mysql判断两个时间段是否有交集
  20. 使用Druid网上监控

热门文章

  1. 2016集训测试赛(二十)Problem B: 字典树
  2. mysql之创建数据库,创建数据表
  3. Android Actionbar 添加返回按钮
  4. C# DBHelper类
  5. zfighting 的问题
  6. 转: https 加密通信流程
  7. linux中xargs用法
  8. .NET创建宿主设计器--DesignHost、DesignSurface.
  9. maven 配置jetty插件
  10. 应该知道的Linux技巧(转载)