当前位置:网站首页>高斯消元
高斯消元
2022-07-07 07:09:00 【moyangxian】
bool dcmp(int x, int y, int k) {
if (fabs(a[x][k]) > fabs(a[y][k]))
return true;
else if (fabs(a[x][k]) < fabs(a[y][k]))
return false;
else {
for (int i = k + 1; i <= n; i++)
if (fabs(a[x][i]) < fabs(a[y][i])) return true;
return false;
}
}
//0表示有无穷解,1表示有唯一解
//测试洛谷P3389
int Gauss() {
for (int r = 1, c = 1; c <= n; c++, r++) {
int t = r;
for (int i = r + 1; i <= n; i++)
if (fabs(a[i][c]) > fabs(a[t][c]))
t = i;
if (sgn(a[t][c]) == 0) return 0;
if (t != r)for (int i = c; i <= n + 1; i++)swap(a[t][i], a[r][i]);
for (int i = n + 1; i >= c; i--)a[r][i] /= a[r][c];
for (int i = 1; i <= n; i++)
if (i != r) {
for (int j = c + 1; j <= n + 1; j++)
a[i][j] -= a[r][j] * a[i][c];
a[i][c] = 0;
}
}
return 1;
}
//-1表示无解,0表示有无穷解,1表示有唯一解
//测试洛谷P2455
int Gauss() {
for (int r = 1, c = 1; c <= n; c++, r++) {
int t = r;
for (int i = r + 1; i <= n; i++)
if (dcmp(i, t, c)) t = i;
if (t != r) for (int i = c; i <= n + 1; i++) swap(a[t][i], a[r][i]);
if (sgn(a[r][c]) == 0) continue;
for (int i = n + 1; i >= c; i--) a[r][i] /= a[r][c];
a[r][c] = 1;
for (int i = 1; i <= n; i++) {
if (i == r) continue;
for (int j = c + 1; j <= n + 1; j++)
a[i][j] -= a[r][j] * a[i][c];
a[i][c] = 0;
}
}
bool f1 = 0, f2 = 0;
for (int i = 1; i <= n; i++) {
if (sgn(a[i][i]) == 0 && sgn(a[i][n + 1]) != 0) f1 = 1;
if (sgn(a[i][i]) == 0 && sgn(a[i][n + 1]) == 0) f2 = 1;
}
if (f1) return -1; //无解
if (f2) return 0; //无穷多解
return 1; //唯一解
}
边栏推荐
- 根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
- 数据建模中利用3σ剔除异常值进行数据清洗
- JS judge whether checkbox is selected in the project
- Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
- How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
- Where is the answer? action config/Interceptor/class/servlet
- How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
- Information Security Experiment 2: using x-scanner scanning tool
- 如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
- The configuration and options of save actions are explained in detail, and you won't be confused after reading it
猜你喜欢
随机推荐
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
【原创】程序员团队管理的核心是什么?
scrapy爬虫mysql,Django等
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
Unity uses mesh to realize real-time point cloud (I)
Lecture 1: stack containing min function
Integer or int? How to select data types for entity classes in ORM
Kubernetes cluster capacity expansion to add node nodes
Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
Diffusion模型详解
Pick up the premise idea of programming
Final keyword
How to speed up video playback in browser
JS inheritance prototype
How to solve the problem of golang select mechanism and timeout
Upload taro pictures to Base64
Database multi table Association query problem
First issue of JS reverse tutorial