當前位置:編程學習大全網 - 編程語言 - C++語言編程 編程求N階行列式的值。

C++語言編程 編程求N階行列式的值。

#include <iostream>

using namespace std;

#define N 3

//input a[N][N]

void input(int a[N][N])

{

cout<<"Please input by rows:"<<endl;

for(int i=0;i<N;i++)

for(int j=0;j<N;j++)

{

cin>>a[i][j];

}

}

//compute the positive value

int PositiveValue(int a[N][N])

{

int product=0,sum=0;

for(int j=0;j<N;j++)

{

//the first value is in ROW0

product=a[0][j];

int k=j+1;

//the next N-1 Row's product

for(int m=1;m<N;m++)

product*=a[m][(k++)%N];

//positive sum

sum+=product;

}

return sum;

}

//compute the negative value

int NegativeValue(int a[N][N])

{

int product=0,sum=0;

for(int j=N-1;j>=0;j--)

{

//the first value is in ROW0

product=a[0][j];

int k=j-1;

//the next N-1 Row's product

for(int m=1;m<N;m++)

product*=a[m][(k--+N)%N];

//positive sum

sum+=product;

}

return sum;

}

//Main

int main()

{

int a[N][N];

int psum=0,nsum=0,sum=0;

input(a);

if(N==1)

sum=a[0][0];

else if(N==2)

{

psum=a[0][0]*a[1][1];

nsum=a[0][1]*a[1][0];

}

else

{

psum=PositiveValue(a);

nsum=NegativeValue(a);

}

sum=psum-nsum;

cout<<"the result is:"<<sum;

return 0;

}

  • 上一篇:JAVA:壹個類實現多個接口
  • 下一篇:青庭編程
  • copyright 2024編程學習大全網