24h購物| | PChome| 登入
2012-12-15 14:34:55| 人氣497| 回應0 | 上一篇 | 下一篇

[UVA][半高斯] 10524 - Matrix Reloaded

推薦 0 收藏 0 轉貼0 訂閱站台

THE SAMS' CONTEST

Problem 5

 Matrix Reloaded 

BACKGROUND

Moni is a brilliant student of BUET. After the last class of the current term (semester) he decided to watch the renowned film of the year 2003 "MATRIX RELOADED" that was displayed in the BUET AUDITORIUM. Yet he could not forget it. He became so much enthusiastic about the subject of Mathematics "Matrix" that he decided to make a computer version of every program of Matrix. As it is a huge subject he wants your help. Please write a program that calculates the inverse of a matrix.

Input

The input consists of several cases. Each case consists of a matrix. Consecutive cases are separated by a black line. Each element of the matrices given are separated from other members by spaces. The first line for each case is a number ,n, giving the order of the matrix. Input is terminated by a value of n=0.

Output

For each case your program should 1) if possible calculate the inverse of the matrix and print it with members of same row separated by a space and columns are printed in separate line 2) otherwise print a line Not possible . Print a black line after each test cases. I think you all know that a singular matrix has no inverse matrix. No pivoting is allowed. For output format see the sample output.

Sample Input

2
1 2
2 1

2
1 2
2 1

0

Sample Output

-0.333333 0.666667
0.666667 -0.333333

-0.333333 0.666667
0.666667 -0.333333

如討論所說,作者需要加強一下英文表達,只有半個高斯啊,不準兩列(row)交換。


#include <stdio.h>
#include <math.h>
#define eps 1e-7
int main() {
int n;
while(scanf("%d", &n) == 1 && n) {
int m = 2*n, i, j, k, ch;
double mx[n][m], tmp, c;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++)
scanf("%lf", &mx[i][j]);
for(j = n; j < m; j++)
mx[i][j] = 0;
mx[i][i+n] = 1;
}
for(i = 0; i < n; i++) {
if(fabs(mx[i][i]) < eps) break;
for(j = 0; j < n; j++) {
if(i == j) continue;
c = mx[j][i]/mx[i][i];
for(k = i; k < m; k++)
mx[j][k] = mx[j][k] - mx[i][k]*c;
}
}
if(i != n) {
puts("Not possible\n");
continue;
}
for(i = 0; i < n; i++, puts("")) {
for(j = n; j < m; j++) {
if(j != n) putchar(' ');
printf("%.6lf", mx[i][j]/mx[i][i]);
}
}
puts("");
}
return 0;
}

台長: Morris
人氣(497) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 10655 - Contemplation - Algebra
此分類上一篇:[UVA][二分+貪婪] 11516 - WiFi

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文