满意答案
wo1q743hom3
2013.04.24
采纳率:46% 等级:12
已帮助:13411人
#include
#include
#include
#include
using namespace std;
double det(int N,vector A)
{
double D=0;
vector B((N-1)*(N-1));
if(N==2)
return A[0]*A[3]-A[1]*A[2];
for(int i=0;i
{
for(int j=1;j
for(int p=1;p
{
for(int h=0;h
{
if(h
B[(p-1)*(N-1)+h]=A[p*N+h];
else if(h>=i)
B[(p-1)*(N-1)+h]=A[p*N+h+1];
}
}
D+=pow(float(-1),i)*A[i]*det(N-1,B);
}
return D;
}
vector adjoint(int N,vector A)
{
vector B(N*N);
vector M((N-1)*(N-1));
for(int p=0;p
{
int cel=p%N;
int row=(p-cel)/N;
for(int i=0;i
for(int j=0;j
{
if(i
M[i*(N-1)+j]=A[i*N+j];
else if(i>=row&&j
M[i*(N-1)+j]=A[(i+1)*N+j];
else if(i=cel)
M[i*(N-1)+j]=A[i*N+j+1];
else
M[i*(N-1)+j]=A[(i+1)*N+j+1];
}
B[cel*N+row]=pow(float(-1),cel+row)*det(N-1,M);
}
return B;
}
void main()
{
cout<
int N;
cin>>N;
cout<
vector A(N*N);
vector B(N*N);
for (int i=0;i
for(int j=0;j
cin>>A[i*N+j];
cout<
for(int i=0;i
{
cout<
if((i+1)%N ==0)
cout<
}
if(det(N,A)==0)
{
cout<
system("pause");
return;
}
B=adjoint(N,A);
cout<
for(int i=0;i
{
B[i]=B[i]/det(N,A);
cout<
if((i+1)%N ==0)
cout<
}
system("pause");
}
C++写的,用了我两个小时,可以求任意阶矩阵的逆,可能有bug,hehe。
10分享举报