p3389 高斯消元模板题(浮点)
作者:互联网
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <map> #include <set> // #include <array> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> // #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define Time (double)clock() / CLOCKS_PER_SEC #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 100 + 20; const int M = 1e5 + 20; const int mod = 1e9 + 7; const double eps = 1e-6; int n; double a[N][N]; int gauss(){ int c, r; for(c = 1, r = 1; c <= n; c ++){ int t = r; for(int i = r + 1; i <= n; i ++){ if(fabs(a[t][c]) < fabs(a[i][c])){ t = i; } } if(fabs(a[t][c]) < eps) continue; 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 = r + 1; i <= n; i ++){ if(fabs(a[r][c]) > eps){ for(int j = n + 1; j >= c; j --){ a[i][j] -= a[i][c] * a[r][j]; } } } r ++; } if(r < n + 1) return -1; for(int i = n; i >= 1; i --){ for(int j = i + 1; j <= n; j ++){ a[i][n + 1] -= a[i][j] * a[j][n + 1]; } } return 1; } void solve() { sd(n); for(int i = 1; i <= n; i ++){ for(int j = 1; j <= n + 1; j ++){ scanf("%lf", &a[i][j]); } } int ans = gauss(); if(ans == -1){ puts("No Solution"); } else{ for(int i = 1; i <= n; i ++){ printf("%.2lf\n", a[i][n + 1]); } } } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/code/out.txt", "w", stdout); // freopen("/home/jungu/code/out.txt","w",stdout); #endif // ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; // sd(T); // init(); // int cas = 1; while (T--) { // printf("Case #%d:", cas++); solve(); } return 0; }
标签:const,int,scanf,浮点,高斯消,--,include,p3389,define 来源: https://www.cnblogs.com/jungu/p/13462081.html