题解 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< ...
随机推荐
- Maximum Product Subarray - LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 不得不知Git远程操作详解
Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...
- cocos3.7.1 mac 创建项目
cocos2d-x-3.7/tools/cocos2d-console/bin目录下,输入命令: ./cocos.py new HelloWorldDemo -p com.coco2dx.org -l ...
- MongoDB下载安装測试及使用
1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...
- setTag和findViewByTag的使用具体解释
在使用ListView或者GridView的时候. 假设想要在Aciviry中获取到Item中的子View,比較频繁的使用是:getChildAt(int position): 之前自己差点儿不会去使 ...
- jquery:给正则表达式添加变量
http://www.2cto.com/kf/201402/277766.html 正则表达式普通用法:var checkString=/^.*\S+.*$/; //注意正则表达式没有引号 chec ...
- ie 浏览器无法保存cookie,且与域名包括了下划线(_)有关系的问题
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 设计模式之MVC设计模式初阶
MVC M:Model(数据) V:View(界面) C:Control(控制) 1⃣️Control可以直接访问View和Model 2⃣️View不可以拥有Control和Model属性,降低耦合 ...
- 我的IT之路
在写这篇文章的时候内心是无比激动,因为这辈子是注定和IT打交道了. 都说大学时光是美好的,但却只有到了大四才知道时间是短暂的,也许和许多人一样,我的大学主要时光是在游戏中度过,1000多把的寒冰算是同 ...
- HttpClient 模拟登录搜狐微博
http://mengyang.iteye.com/blog/575671 第一次遇到一个这样的问题,"PKIX path building failed" 异常 详解异常: ...