C语言编程求逆矩阵,c语言求逆矩阵

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

wo1q743hom3

2013.04.24

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率: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分享举报

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值