10309 - Inverse Matrix   

Description

An nxn square matrix A is called invertible or non-singular if there exists a matrix B such that AB = BA = In. If B exists, it is unique and is called the inverse matrix of A, denoted A−1. In this problem, you are given a 3x3 matrix A, and supposed to calculate A−1.

For example, for

 

Note that the denominator should be positive and each element should be expressed in simplest terms.


 The formulation of inverse matrices: 

 

 

The following is an excerpt of incomplete code:

#include

int a[3][3]={0},nu[3][3]={0},de[3][3]={0},div[3][3]={0};
//a[3][3] is input, nu[3][3] is the numerator of output, de[3][3] is the denominator of input, div[3][3] is gcd of nu[3][3] and de[3][3]
int i,j,det=0;
void show();
void simple();
int gcd(int x,int y);

int main(void)
{
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            scanf("%d",&a[i][j]);
        }
    }

    /* your code */

    simple();
    show();

    return 0;
}

void show(){
    for(i=0;i<3;i++){
        for(j=0;j<3;j++)
            printf("%4d",nu[i][j]/div[i][j]);
        printf("\n --- --- ---\n");
        for(j=0;j<3;j++)
            printf("%4d",de[i][j]/div[i][j]);
        printf("\n\n");
    }
}

void simple(){
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            if(nu[i][j]!=0)
                if(de[i][j]>0)
                    div[i][j]=gcd(abs(nu[i][j]),abs(de[i][j]));
                else
                    div[i][j]=-gcd(abs(nu[i][j]),abs(de[i][j]));
            else{
                de[i][j]=0;
                div[i][j]=1;
            }
        }
    }
}

int gcd(int x,int y){
    if(x%y==0) return y;
    else return gcd(y,x%y);
}

 

Input

A 3x3 matrix A.

 

Output

The inverse matrix of A, denoted A−1.

 

Sample Input  Download

Sample Output  Download

Tags




Discuss