#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <time.h>
#include <iostream>
using namespace std;
int sum;
int sum1,sum2;
int **a,**b;
int n;
CRITICAL_SECTION cs;
HANDLE evFin[2];
int power(int order){
if(order % 2 == 0)
return 1;
else return -1;
}
int cal_matrix(int** m, int n, int row, int col){
int** temp;
int sum = 0;
int temp_row = 0, temp_col = 0;
// EnterCriticalSection(&cs);
if (n == 0)
return sum = 1;
temp = new int* [n];
for (int i = 0; i < n ;i++)
{
temp[i] = new int[n];
}
for (int i = 0 ; i <= n; i++){
if (i == row - 1)
continue;
for (int j = 0;j <= n; j++){
if (j != col - 1){
if (temp_col < n){
//EnterCriticalSection(&cs);
temp[temp_row][temp_col++] = m[i][j];
// LeaveCriticalSection(&cs);
}
else{
temp_col = 0;
temp_row++;
temp[temp_row][temp_col] = m[i][j];
temp_col++;
}
}
}
}
/************************調試輸出行列式*******************************/
/*********************************************************************/
/*
for(int i = 0; i< n; i++)
for(int j = 0; j < n; j++)
cout<<temp[i][j];
cout<<" ";
*/
/*********************************************************************/
for (int i = 0; i < n; i++){
sum += temp[row-1][i] * power(row+i+1) * cal_matrix(temp,n-1,row,i+1);
}
return sum;
delete[] temp;
//LeaveCriticalSection(&cs);
}
void ThreadFunc1(PVOID param){
for (int i = 0; i < n/2; i++){
sum1 += a[0][i] * power(1+i+1) * cal_matrix(a,n-1,1,i+1);
}
//SetEvent(evFin[0]);
}
void ThreadFunc2(PVOID param){
for(int i = n/2; i < n; i++)
sum2 += a[0][i] * power(1+i+1) * cal_matrix(a,n-1,1,i+1);
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handle[2];
clock_t start ,finish;
//evFin[0] = CreateEvent(NULL,FALSE,FALSE,NULL);
// evFin[0] = CreateEvent(NULL,FALSE,FALSE,NULL);
cout<<"The degree of Matrix: "<<endl;
cin>>n;
a= new int* [n];
for (int i = 0; i < n ;i++)
{
a[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin>>a[i][j];
}
}
start = clock();
/******************************單進程********************************/
/********************************************************************/
/***
for(int i = 0; i < n; i++){
sum += a[0][i] * power(1+i+1) * cal_matrix(a,n-1,1,i+1);
}
*/
/*********************************************************************/
handle[0] = (HANDLE)_beginthread(ThreadFunc1,0,NULL);
handle[1] = (HANDLE)_beginthread(ThreadFunc2,0,NULL);
//Sleep(1000);
WaitForMultipleObjects(2,handle,TRUE, INFINITE);
sum = sum1 + sum2;
cout<<sum<<endl;
finish = clock();
cout<<"Program is running:"<<(double)(finish - start)/ CLOCKS_PER_SEC<<"s"<<endl;
delete[] a;
return 0;
}
遞歸求解N階行列式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.