Loading

高斯消元模板

double a[maxn][maxn];

int n;
void Gauss() {
    for (int i = 1; i <= n; i++) {
        int Max = i;
        for (int j = i + 1; j <= n; j++) {
            if (fabs(a[j][i]) > fabs(a[Max][i])) Max = j;
        }
        for (int j = 1; j <= n + 1; j++) swap(a[i][j], a[Max][j]);
        if (!a[i][i]) { puts("No Solution"); return; }
        for (int j = 1; j <= n; j++) {
            if (j != i) {
                double tmp = a[j][i] / a[i][i];
                for (int k = i + 1; k <= n + 1; k++) a[j][k] -= a[i][k] * tmp;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        printf("%.2f\n", a[i][n + 1] / a[i][i]);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) for (int j = 1; j <= n + 1; j++) scanf("%lf", &a[i][j]);
    Gauss();
}

 

posted @ 2020-07-17 09:03  MQFLLY  阅读(151)  评论(0编辑  收藏  举报