bzoj1013 Gaussian elimination

Subject: https://www.lydsy.com/JudgeOnline/problem.php?id=1013

It seems to be obvious Gaussian elimination;

The first time to write Gaussian elimination.

code show as below:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-6;
using namespace std;
int n;
double a[15][15],d[15];
void gauss()
{
    for(int i=1;i<=n;i++)
    {
        int t=i;
        for(int j=i+1;j<=n;j++)
            if(fabs(a[j][i])>fabs(a[t][i]))t=j;
        if(t!=i)for(int j=1;j<=n+1;j++)
                    swap(a[i][j],a[t][j]); // Let the i-th line represent the i-th unknown 
        double tmp=a[i][i]; // !!!Can't be directly on the next line Write a[i][j]/=a[i][i], because a[i][i] is changed in the middle 
        for ( int j= 1 ;j<=n+ 1 ;j++)a[i][ j]/=tmp; // Coefficient to 1 
        for ( int j= 1 ;j<=n;j++ )
        {
            if(i==j)continue;
            double x=a[j][i];
            for(int k=1;k<=n+1;k++)
                a[j][k]-=x*a[i][k];
        }
    }
    for(int i=1;i<n;i++)
        printf("%.3lf ",a[i][n+1]);
    printf("%.3lf\n",a[n][n+1]);
}
intmain ()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lf",&d[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            double x;
            scanf("%lf",&x);
            a[i][j]=2*(x-d[j]);
            a[i][n+1]+=x*x-d[j]*d[j];
        }
    gauss();
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214434&siteId=291194637