本文实例讲述了java实现的n*n矩阵求值及求逆矩阵算法。分享给大家供大家参考,具体如下:
先来看看运行结果:
java版的写出来了,用的跟c语言相同的算法,然后看看能不能以后加个框做成程序:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
import java.math.*; import java.util.*; import java.text.*; public class matrix { static int map1[][]= new int [ 110 ][ 110 ]; static int just[][]= new int [ 110 ][ 110 ]; public static void printf( int n, int map[][]) { int i,j; for (i= 1 ;i<=n;i++ ) { for (j= 1 ;j<n;j++) system.out.print(map[i][j]+ " " ); system.out.println(map[i][j]); } } public static void get( int numi, int numj, int map[][], int n) { int i,j,k,l; for (i= 0 ;i<n+ 10 ;i++) for (j= 0 ;j<n+ 10 ;j++) just[i][j]= 1 ; for (i= 1 ;i<=n- 1 ;i++) //求余子式矩阵 for (j= 1 ;j<=n- 1 ;j++) { if (i>=numi&&j<numj) just[i][j]=map[i+ 1 ][j]; else if (i>=numi&&j>=numj) just[i][j]=map[i+ 1 ][j+ 1 ]; else if (i<numi&&j>=numj) just[i][j]=map[i][j+ 1 ]; else if (i<numi&&j<numj) just[i][j]=map[i][j]; } } //static int map[][]=new int [110][110]; public static int getans( int nn) { int map[][]= new int [ 110 ][ 110 ]; for ( int i= 1 ;i<=nn;i++) for ( int j= 1 ;j<=nn;j++) map[i][j]=just[i][j]; if (nn== 2 ) return map[ 1 ][ 1 ]*map[ 2 ][ 2 ]-map[ 1 ][ 2 ]*map[ 2 ][ 1 ]; else if (nn== 1 ) return map[ 1 ][ 1 ]; else { int cnb= 0 ; for ( int i= 1 ;i<=nn;i++) { get( 1 ,i,map,nn); //得到当前余子式 just // printf("pay attention!\n"); //print(map,nn); //print(just,nn-1); if (i% 2 == 1 ) cnb+=map[ 1 ][i]*getans(nn- 1 ); else cnb-=map[ 1 ][i]*getans(nn- 1 ); } return cnb; } } public static int gcd( int m, int n) { // if(m<n) // matrix.gcd(n,m); // if(n==0) // return m; // // else // return matrix.gcd(n,m%n); int mm=m; int nn=n; if (mm<nn) { int c=mm; mm=nn; nn=c; } int w= 1 ; while (w!= 0 ) { w=mm%nn; mm=nn; nn=w; } return mm; } public static void ans( int n, int m) { if (n*m< 0 ) { system.out.print( "-" ); ans(math.abs(n),math.abs(m)); return ; } if (m== 1 ) system.out.print(n+ "\t" ); else if (n%m== 0 ) system.out.print(n/m+ "\t" ); else system.out.print((n/matrix.gcd(m, n))+ "/" +(m/matrix.gcd(m, n))+ "\t" ); } public static void main(string[] args) { // todo 自动生成的方法存根 scanner cin= new scanner(system.in); int i,j,k,l,m,p; while ( true ) { int n=cin.nextint(); int ans= 0 ; for (i= 0 ;i<n+ 10 ;i++) for (j= 0 ;j<n+ 10 ;j++) map1[i][j]= 1 ; for (i= 1 ;i<=n;i++) for (j= 1 ;j<=n;j++) { map1[i][j]=cin.nextint(); just[i][j]=map1[i][j]; } int ans1=matrix.getans(n); system.out.println( "矩阵的值为:" ); system.out.println(ans1); int map2[][]= new int [ 110 ][ 110 ]; for (i= 1 ;i<=n;i++) for (j= 1 ;j<=n;j++) { map2[i][j]=map1[j][i]; just[i][j]=map2[i][j]; } system.out.println( "转置矩阵为:" ); matrix.printf(n, map2); int help2=matrix.getans(n); system.out.println(help2); if (help2== 0 ) { system.out.println( "no inverse matrix" ); continue ; } system.out.println( "逆矩阵为:" ); for (i= 1 ;i<=n;i++) { for (j= 1 ;j<=n;j++) { matrix.get(i, j, map2, n); //boolean b=((i+j)%2==0); if ((i+j)% 2 == 0 ) matrix.ans(matrix.getans(n- 1 ), help2); else matrix.ans(matrix.getans(n- 1 )*- 1 , help2); } system.out.println(); } system.out.println(); } } } |
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/z8110/article/details/51691577