标签:
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=625
题意:给出一个n阶行列式,求值。
思路:模板参考http://blog.csdn.net/zhoufenqin/article/details/7779707:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 int n; 6 int a[35][35]; 7 void determinant() 8 { 9 int cnt = 0; 10 int ans = 1; 11 for(int i = 1; i <= n; i++) 12 { 13 for(int j = i+1; j <= n; j++) 14 { 15 int x, y; 16 x = i, y = j; 17 while(a[y][i]) 18 { 19 int t = a[x][i] / a[y][i]; 20 for(int k = 1; k <= n; k++) 21 { 22 a[x][k] = a[x][k] - a[y][k]*t; 23 } 24 swap(x, y); 25 } 26 if(x != i) 27 { 28 for(int k = 1; k <= n; k++) 29 { 30 swap(a[x][k], a[y][k]); 31 } 32 cnt ^= 1; 33 } 34 } 35 if(a[i][i] == 0) 36 { 37 ans = 0; break; 38 } 39 else ans *= a[i][i]; 40 } 41 if(cnt) ans *= -1; 42 printf("%d\n", ans); 43 } 44 int main() 45 { 46 // freopen("in.txt", "r", stdin); 47 while(scanf("%d", &n)) 48 { 49 if(n == 0) 50 { 51 printf("*\n"); 52 break; 53 } 54 for(int i = 1; i <= n; i++) 55 { 56 for(int j = 1; j <= n; j++) 57 { 58 scanf("%d", &a[i][j]); 59 } 60 } 61 determinant(); 62 } 63 return 0; 64 }
uva 684 Integral Determinant 行列式求值
标签:
原文地址:http://www.cnblogs.com/titicia/p/5297599.html