bzoj1013

这道题题解太多,只贴代码。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std ;  
 5 
 6 const int MAXN = 50 ; 
 7 double value [ MAXN ] [ MAXN ] ; 
 8 double ans [ MAXN ] ;
 9 int N ; 
10 
11 void print () {
12     for ( int i = 0 ; i < N ; ++ i ) {
13         for ( int j = 0 ; j <= N ; ++ j ) printf ( "%.6lf " , value [ i ] [ j ] ) ;
14         putchar ( '\n' ) ;
15     }
16     putchar ( '\n' ) ;
17 }
18 
19 void Gauss () {
20     for ( int i = 0 ; i < N ; ++ i ) {
21         {
22             int p = i ; 
23             for ( int j = i + 1 ; j < N ; ++ j ) 
24                 if ( fabs ( value [ p ] [ i ] ) < fabs ( value [ j ] [ i ] ) ) p = j ;
25             swap_ranges ( value [ i ] , value [ i ] + N + 1 , value [ p ] ) ;
26         }
27         {
28             const double d = 1.0 / value [ i ] [ i ] ; 
29             for ( int j = 0 ; j <= N ; ++ j ) value [ i ] [ j ] *= d ;
30         }
31         for ( int j = 0 ; j < N ; ++ j ) {
32             if ( j == i ) continue ; 
33             const double d = value [ j ] [ i ] ;
34             for ( int k = 0 ; k <= N ; ++ k ) value [ j ] [ k ] -= d * value [ i ] [ k ] ;
35         }
36     }
37 }
38 
39 double x [ MAXN ] [ MAXN ] ;
40 int main () {
41     scanf ( "%d" , & N ) ;
42     for ( int i = 0 ; i <= N ; ++ i ) 
43         for ( int j = 0 ; j < N ; ++ j ) scanf ( "%lf" , & x [ i ] [ j ] ) ;
44     for ( int i = 0 ; i < N ; ++ i ) x [ i ] [ N ] = 0.0 ;
45     for ( int i = 0 ; i < N ; ++ i ) {
46         for ( int j = 0 ; j < N ; ++ j ) {
47             value [ i ] [ j ] = 2 * ( x [ i ] [ j ] - x [ i + 1 ] [ j ] ) ;
48             value [ i ] [ N ] += x [ i ] [ j ] * x [ i ] [ j ] - x [ i + 1 ] [ j ] * x [ i + 1 ] [ j ] ;
49         }
50     }
51     Gauss () ;
52     printf ( "%.3lf" , value [ 0 ] [ N ] ) ;
53     for ( int i = 1 ; i < N ; ++ i ) printf ( " %.3lf" , value [ i ] [ N ] ) ;
54     return 0 ;
55 }
56 
57 
58     

 

你可能感兴趣的:(bzoj1013)