eternal @ 2018-07-04 14:05:50
#include <bits/stdc++.h>
#define rep( i , l , r ) for( int i = (l) ; i <= (r) ; ++i )
#define per( i , r , l ) for( int i = (r) ; i >= (l) ; --i )
#define erep( i , u ) for( int i = head[(u)] ; ~i ; i = e[i].nxt )
using namespace std;
int _read(){
char ch = getchar();
int x = 0 , f = 1 ;
while( !isdigit( ch ) )
if( ch == '-' ) f = -1 , ch = getchar();
else ch = getchar();
while( isdigit( ch ) )
x = (ch - '0') + x * 10 , ch = getchar();
return x * f;
}
const int maxn = 50 + 5;
int a[maxn][maxn] , N;
double x[maxn];
int _gcd( int a , int b ) { int t; for( ; b ; t = b , b = a % b , a = t ); return a; }
int _lcm( int a , int b ) { return a / _gcd( a , b ) * b; }
int Guass(){
int max_r = 0 , col = 1 , k; // col 表示当前的处理的列
for( k = 1 ; k <= N && col <= N ; ++k , ++col ){
max_r = k;
rep( i , k + 1 , N )
if( abs(a[i][col]) > abs(a[max_r][col]) ) max_r = i;
if( max_r != k ) rep( j , 1 , N + 1 ) swap( a[max_r][j] , a[k][j] );
if( 0 == a[k][col] ) { --k ; continue; }
int t1 , t2;
rep( i , k + 1 , N ){
if( a[i][col] != 0 ){
int lcm = _lcm( abs( a[i][col] ) , abs( a[k][col] ) );
t1 = lcm / abs( a[i][col] );
t2 = lcm / abs( a[k][col] );
if( a[i][col] * a[k][col] < 0 ) t2 = -t2;
rep( j , col , N + 1 )
a[i][j] = a[i][j] * t1 - a[k][j] * t2;
}
}
}
rep( i , k , N ) if( a[i][col] != 0 ) return -1;
if( k <= N ) return 0;
per( i , N , 1 ){
double t = (double)a[i][N + 1];
rep( j , i + 1 , N ){
if( a[i][j] ) t -= (double)a[i][j] * x[j];
}
x[i] = t / a[i][i];
}
return 1;
}
int main(){
scanf("%d" , &N);
rep( i , 1 , N )
rep( j , 1 , N + 1 )
scanf("%d", &a[i][j]);
int flg = Guass();
if( flg == -1 ) puts("-1");
else if( flg == 0 ) puts("0");
else{
rep( i , 1 , N )
printf("x%d=%.2lf\n" , i , x[i] );
}
return 0;
}
有两个点显示too many / few lines 看不清情况,不清楚是什么问题,希望大佬能帮忙看一下,谢谢了。