题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1013
第一次打高斯消元,其实挺容易的。。。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std ;
#define MAXN 20
double pos[ MAXN ][ MAXN ] , mat[ MAXN ][ MAXN ] , Ans[ MAXN ] ;
int n ;
void Solve( ) {
for ( int i = 0 ; i ++ < n - 1 ; ) {
for ( int j = i ; j ++ < n ; ) {
if ( mat[ j ][ i ] > mat[ i ][ i ] ) {
for ( int k = 0 ; k ++ < n + 1 ; ) {
swap( mat[ i ][ k ] , mat[ j ][ k ] ) ;
}
}
}
for ( int j = i ; j ++ < n ; ) {
double ret = mat[ i ][ i ] / mat[ j ][ i ] ;
for ( int k = 0 ; k ++ < n + 1 ; ) {
mat[ j ][ k ] *= ret ;
mat[ j ][ k ] -= mat[ i ][ k ] ;
}
}
}
Ans[ n ] = mat[ n ][ n + 1 ] / mat[ n ][ n ] ;
for ( int i = n - 1 ; i ; -- i ) {
for ( int j = i ; j ++ < n ; ) {
mat[ i ][ n + 1 ] -= mat[ i ][ j ] * Ans[ j ] ;
}
Ans[ i ] = mat[ i ][ n + 1 ] / mat[ i ][ i ] ;
}
}
int main( ) {
scanf( "%d" , &n ) ;
for ( int i = 0 ; i ++ < n + 1 ; ) {
for ( int j = 0 ; j ++ < n ; ) {
scanf( "%lf" , &pos[ i ][ j ] ) ;
}
}
memset( mat , 0 , sizeof( mat ) ) ;
for ( int i = 0 ; i ++ < n ; ) {
for ( int j = 0 ; j ++ < n ; ) {
mat[ i ][ j ] = 2 * ( pos[ i ][ j ] - pos[ i + 1 ][ j ] ) ;
mat[ i ][ n + 1 ] += ( pos[ i ][ j ] * pos[ i ][ j ] ) ;
mat[ i ][ n + 1 ] -= ( pos[ i + 1 ][ j ] * pos[ i + 1 ][ j ] ) ;
}
}
Solve( ) ;
for ( int i = 0 ; i ++ < n - 1 ; ) printf( "%.3f " , Ans[ i ] ) ;
printf( "%.3f\n" , Ans[ n ] ) ;
return 0 ;
}