-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetermine.cpp
More file actions
76 lines (74 loc) · 1.65 KB
/
determine.cpp
File metadata and controls
76 lines (74 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int find_order(int **martix,int order){
int result = 0;
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
if(martix[i][j]!= 0 )
{
result++;
break;
}
}
}
return result;
}
int **find_rest(int **martix,int order,int x){
int **result_martix;
result_martix = (int**)malloc((order-1)*sizeof(int*));
for(int i = 0;i<order-1;i++)
{
result_martix[i] = (int*)malloc((order-1)*sizeof(int));
memset(result_martix[i],0,order-1);
}
//fill the martix
for(int i=1;i<order;i++)
{
for(int j=0,j_pointer=0;j<order;j++)
{
if(j==x) continue;
int temp = martix[i][j];
result_martix[i-1][j_pointer++] = temp;
}
}
return result_martix;
}
int determine(int **martix,int initial_order){
int sum=0,order=find_order(martix,initial_order);
if(order>2){
for(int i=0;i<order;i++)
{
if(i%2==0) sum += martix[0][i]*determine(find_rest(martix,order,i),order-1);
else sum -= martix[0][i]*determine(find_rest(martix,order,i),order-1);
}
}
else if (order==2){
sum = (martix[0][0]*martix[1][1]-martix[1][0]*martix[0][1]);
}
return sum;
}
int main()
{
int **martix;
int order = 0;
printf("please input the order(in the range of 100):");
scanf("%d",&order);
printf("please input your martix:\n") ;
martix = (int **)malloc(order*sizeof(int*));
for(int cnt=0;cnt<order;cnt++)
{
martix[cnt]=(int *)malloc(order*sizeof(int));
}
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
scanf("%d",&martix[i][j]);
}
}
printf("the determine is %d\n",determine(martix,order));
return 0;
}