线性代数高斯消元(显式) 算法步骤: 枚举每一列C 1、找当前列绝对值最大的一行 2、将该行换到最上面的未处理行 3、将该行的第一个数变成1 4、将下面所有行的第C列清成0 代码实现
int guass ( )
{
int c
int r
for ( c= 0 , r= 0 ; c< n; c++ )
{
int t= r;
for ( int i= r; i< n; i++ )
{
if ( fabs ( a[ i] [ c] ) > fabs ( a[ t] [ c] ) )
t= i;
}
if ( fabs ( a[ t] [ c] ) < esp) continue ;
for ( int i= c; i<= n; i++ ) swap ( a[ t] [ i] , a[ r] [ i] ) ;
for ( int i= n; i>= c; i-- ) a[ r] [ i] / = a[ r] [ c] ;
for ( int i= r+ 1 ; i< n; i++ )
{
if ( fabs ( a[ i] [ c] ) > esp)
{
for ( int j= n; j>= c; j-- )
{
a[ i] [ j] - = a[ r] [ j] * a[ i] [ c] ;
}
}
}
r++ ;
}
if ( r< n)
{
for ( int i= r; i< n; i++ )
{
if ( fabs ( a[ i] [ n] > esp) )
return 2 ;
}
return 1 ;
}
for ( int i= n- 1 ; i>= 0 ; i-- )
{
for ( int j= i+ 1 ; j< n; j++ )
a[ i] [ n] - = a[ i] [ j] * a[ j] [ n] ;
}
return 0 ;
}
int main ( )
{
cin>> n;
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< n+ 1 ; j++ )
cin>> a[ i] [ j] ;
}
if ( guass ( ) == 0 )
{
for ( int i= 0 ; i< n; i++ ) printf ( "%.2lf\n" , a[ i] [ n] ) ;
}
else if ( guass ( ) == 1 ) puts ( "Infinite group solutions" ) ;
else puts ( "No solution" ) ;
return 0 ;
}