这道题题解太多,只贴代码。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std ; const int MAXN = ;
double value [ MAXN ] [ MAXN ] ;
double ans [ MAXN ] ;
int N ; void print () {
for ( int i = ; i < N ; ++ i ) {
for ( int j = ; j <= N ; ++ j ) printf ( "%.6lf " , value [ i ] [ j ] ) ;
putchar ( '\n' ) ;
}
putchar ( '\n' ) ;
} void Gauss () {
for ( int i = ; i < N ; ++ i ) {
{
int p = i ;
for ( int j = i + ; j < N ; ++ j )
if ( fabs ( value [ p ] [ i ] ) < fabs ( value [ j ] [ i ] ) ) p = j ;
swap_ranges ( value [ i ] , value [ i ] + N + , value [ p ] ) ;
}
{
const double d = 1.0 / value [ i ] [ i ] ;
for ( int j = ; j <= N ; ++ j ) value [ i ] [ j ] *= d ;
}
for ( int j = ; j < N ; ++ j ) {
if ( j == i ) continue ;
const double d = value [ j ] [ i ] ;
for ( int k = ; k <= N ; ++ k ) value [ j ] [ k ] -= d * value [ i ] [ k ] ;
}
}
} double x [ MAXN ] [ MAXN ] ;
int main () {
scanf ( "%d" , & N ) ;
for ( int i = ; i <= N ; ++ i )
for ( int j = ; j < N ; ++ j ) scanf ( "%lf" , & x [ i ] [ j ] ) ;
for ( int i = ; i < N ; ++ i ) x [ i ] [ N ] = 0.0 ;
for ( int i = ; i < N ; ++ i ) {
for ( int j = ; j < N ; ++ j ) {
value [ i ] [ j ] = * ( x [ i ] [ j ] - x [ i + ] [ j ] ) ;
value [ i ] [ N ] += x [ i ] [ j ] * x [ i ] [ j ] - x [ i + ] [ j ] * x [ i + ] [ j ] ;
}
}
Gauss () ;
printf ( "%.3lf" , value [ ] [ N ] ) ;
for ( int i = ; i < N ; ++ i ) printf ( " %.3lf" , value [ i ] [ N ] ) ;
return ;
}