题解 P3389 【【模板】高斯消元法】
题解 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;//功德圆满
}
题解 P3389 【【模板】高斯消元法】的更多相关文章
- 字符串 kmp算法 codeforce 625B 题解(模板)
题解:kmp算法 代码: #include <iostream>#include <algorithm>#include <cstring>#include < ...
- CCF-CSP题解 201509-3 模板生成系统
简单的替换一下字符串. 注意数组开大点. #include<bits/stdc++.h> const int maxm = 100; const int maxn = 100; using ...
- 【刷题】洛谷 P3804 【模板】后缀自动机
题目描述 给定一个只包含小写字母的字符串 \(S\) , 请你求出 \(S\) 的所有出现次数不为 \(1\) 的子串的出现次数乘上该子串长度的最大值. 输入输出格式 输入格式: 一行一个仅包含小写字 ...
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- [UOJ#34]多项式乘法
[UOJ#34]多项式乘法 试题描述 这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入 第一行两个整数 n 和 m,分别表示两个多项式的次数. 第二行 n+1 个整数,分别表示第一个多 ...
- 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 10059 Solved: 4863[Submit ...
- 【网络流】POJ1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78671 Accepted: 3068 ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
随机推荐
- luogu P1345 [USACO5.4]奶牛的电信Telecowmunication
https://www.luogu.org/problemnew/show/1345 拆点,中间建流量为1的边,跑最小割 #include<cstdio> #include<queu ...
- saltstack安装+基本命令
环境: node1:172.16.1.60 OS:centos 7.3 master hostname:centos7u3-1 node2:172.16.1.61 OS:centos 7.3 mini ...
- x86服务器中网络性能分析与调优 转
x86服务器中网络性能分析与调优 2017-04-05 巨枫 英特尔精英汇 [OpenStack 易经]是 EasyStack 官微在2017年新推出的技术品牌,将原创技术干货分享给您,本期我们讨论 ...
- 在线更新dede程序后 网站出现错误 DedeCMS Error:Tag disabled:"php" more...!
dedecms出现DedeCMS Error:Tag disabled:php原因解决 ------------------------------------------------------- ...
- 单源最短路Dijstra算法
Dijstra算法是寻找从某一顶点i出发到大其他顶点的最短路径.Distra算法的思想与Prim算法很像,它收录顶点的规则是按照路径长度递增的顺序收录的.设v0是源顶点,我们要寻找从v0出发到其他任意 ...
- Windows脚本\批处理命令学习笔记
1.为新建变量赋值: set 变量=值 2.输出变量的值 echo %变量% 3.关闭批处理中命令行的显示(默认是显示命令行的) 在文件開始处增加:echo off 若需又一次显示:echo on 若 ...
- 七天学会ASP.NET MVC(七)——创建单页应用 【转】
http://www.cnblogs.com/powertoolsteam/p/MVC_Seven.html 系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学 ...
- 实例化Spring容器的两种常用方式
//在类路径下寻找配置文件来实例化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"b ...
- 字符集研究之多字节字符集和unicode字符集
作者:朱金灿 来源:http://blog.csdn.net/clever101 本文简介计算机中两大字符集:多字节字符集和unicode字符集的出现及关系. 首先我们须要明确的是计算机是怎样找到字符 ...
- Esper epl语句实验
基础代码见下,下文列举的实验都是在此程序基础上改动. all,snapshot,first String epl = "select * from appTable.win:time(5 s ...