当前位置:网站首页>Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
2022-07-03 08:51:00 【T_ Y_ F666】
Gauss elimination AcWing 883. Gauss elimination solves linear equations
Original link
AcWing 883. Gauss elimination solves linear equations
Algorithm tags
linear algebra Gauss elimination
Ideas 
Code
#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(){// Gauss elimination , The answer lies in a[i][n] in ,0 <= i < n
// c Representative column r On behalf of the line
int c, r;
// Enumerate by column
for(c=0, r=0; c<n; ++c){
int t=r;
rep(i, r, n){
if(abs(a[i][c])>abs(a[t][c])){// Find the row with the largest absolute value
t=i;
}
}
if(abs(a[t][c])<eps){
continue;
}
rep(i, c, n+1){// Change the line with the largest absolute value to the top
swap(a[t][i], a[r][i]);
}
Rep(i, n, c){// Change the first place of the current line to 1
a[r][i]/=a[r][c];
}
rep(i, r+1, n){// Use the current row to eliminate all the following columns into 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;// unsolvable
}
}
return 1;// There are infinite sets of solutions
}
Rep(i, n-1, 0){
rep(j, i+1, n){
a[i][n]-=a[i][j]*a[j][n];
}
}
return 0;// There is a unique solution
}
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){// Remove the output -0.00 The situation of
a[i][n]=0;
}
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- OpenGL learning notes
- First Servlet
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- UE4 source code reading_ Mobile synchronization
- 单调栈-503. 下一个更大元素 II
- 记忆化搜索 AcWing 901. 滑雪
- Facial expression recognition based on pytorch convolution -- graduation project
- Servlet的生命周期
- 樹形DP AcWing 285. 沒有上司的舞會
- [concurrent programming] explicit lock and AQS
猜你喜欢

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

Es8 async and await learning notes

请求参数的发送和接收

22-06-27 Xian redis (01) commands for installing five common data types: redis and redis

22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

樹形DP AcWing 285. 沒有上司的舞會

Concurrent programming (VI) ABA problems and solutions under CAS

数据库原理期末复习

Six dimensional space (C language)
随机推荐
Solution of 300ms delay of mobile phone
Concurrent programming (VI) ABA problems and solutions under CAS
二进制转十进制,十进制转二进制
Try to reprint an article about CSDN reprint
Apache startup failed phpstudy Apache startup failed
【Rust 笔记】07-结构体
樹形DP AcWing 285. 沒有上司的舞會
[rust notes] 09- special types and generics
MySQL index types B-tree and hash
Unity multi open script
20220630学习打卡
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
【Rust 笔记】08-枚举与模式
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
The method for win10 system to enter the control panel is as follows:
UE4 source code reading_ Bone model and animation system_ Animation process
[rust notes] 13 iterator (Part 1)
【Rust笔记】05-错误处理
UE4 source code reading_ Bone model and animation system_ Animation node
Markdown learning