当前位置:网站首页>高斯消元 AcWing 883. 高斯消元解线性方程组
高斯消元 AcWing 883. 高斯消元解线性方程组
2022-07-03 08:41:00 【T_Y_F666】
高斯消元 AcWing 883. 高斯消元解线性方程组
原题链接
算法标签
线性代数 高斯消元
思路
代码
#include<bits/stdc++.h>
#define int long long
#define abs fabs
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
double a[N][N], eps = 1e-8;
int n;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
int gu(){// 高斯消元,答案存于a[i][n]中,0 <= i < n
// c代表列 r代表行
int c, r;
// 按列枚举
for(c=0, r=0; c<n; ++c){
int t=r;
rep(i, r, n){
if(abs(a[i][c])>abs(a[t][c])){// 找绝对值最大的行
t=i;
}
}
if(abs(a[t][c])<eps){
continue;
}
rep(i, c, n+1){// 将绝对值最大的行换到最顶端
swap(a[t][i], a[r][i]);
}
Rep(i, n, c){// 将当前行的首位变成1
a[r][i]/=a[r][c];
}
rep(i, r+1, n){// 用当前行将下面所有的列消成0
if(abs(a[i][c])>eps){
Rep(j, n, c){
a[i][j]-=a[r][j]*a[i][c];
}
}
}
++r;
}
if(r<n){
rep(i, r, n){
if(abs(a[i][n])>eps){
return 2;// 无解
}
}
return 1;// 有无穷多组解
}
Rep(i, n-1, 0){
rep(j, i+1, n){
a[i][n]-=a[i][j]*a[j][n];
}
}
return 0;// 有唯一解
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read();
rep(i, 0, n){
rep(j, 0, n+1){
scanf("%lf", &a[i][j]);
}
}
int t=gu();
if(t==2){
puts("No solution");
}else if(t==1){
puts("Infinite group solutions");
}else{
rep(i, 0, n){
if(abs(a[i][n])<eps){// 去掉输出 -0.00 的情况
a[i][n]=0;
}
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Unity editor expansion - scrolling list
- Binary to decimal, decimal to binary
- Vscode, idea, VIM development tool shortcut keys
- Deep parsing (picture and text) JVM garbage collector (II)
- Unity Editor Extension - drag and drop
- Location of package cache downloaded by unity packagemanager
- Annotations simplify configuration and loading at startup
- 100 GIS practical application cases (78) - Multi compliance database design and data warehousing
- MySQL containerization (1) docker installation MySQL
- [concurrent programming] Table hopping and blocking queue
猜你喜欢

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

Unity learning notes

VIM learning notes from introduction to silk skating

Deeply understand the underlying data structure of MySQL index

Markdown learning
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism

二进制转十进制,十进制转二进制

22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
随机推荐
C language student management system based on linked list, super detailed
Markdown learning
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
php public private protected
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
[rust notes] 07 structure
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Unity notes 1
[rust notes] 08 enumeration and mode
Unity editor expansion - the framework and context of unity imgui
Notes and bugs generated during the use of h:i:s and y-m-d
[concurrent programming] thread foundation and sharing between threads
[concurrent programming] concurrent security
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Analysis of Alibaba canal principle
Monotonic stack -84 The largest rectangle in the histogram
Downward compatibility and upward compatibility
Osgearth topographic shading map drawing
Final review of Database Principles
Explain sizeof, strlen, pointer, array and other combination questions in detail