-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopactor_expansion_2.cpp
More file actions
73 lines (57 loc) · 1.66 KB
/
Copy pathcopactor_expansion_2.cpp
File metadata and controls
73 lines (57 loc) · 1.66 KB
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
#include <iostream>
#include <string>
#include <numeric>
#include <vector>
#include <sstream>
using namespace std;
vector<string> splitByComma(const string &sentence) {
vector<string> parts;
stringstream ss(sentence);
string part;
while (getline(ss, part, ',')) {
parts.push_back(part);
}
return parts;
}
vector<vector<int>> cofacterer(vector<vector<int>> matrics,int splitnum){
matrics.erase(matrics.begin());
for (int k=0;k<matrics.size();k++){
matrics[k].erase(matrics[k].begin()+splitnum);
}
return matrics;
}
int expansionner(vector<vector<int>> matrics){
if (matrics.size()==2){
return matrics[0][0]*matrics[1][1]-matrics[0][1]*matrics[1][0];
}
else{
int expresult =0;
for (int k = 0; k < matrics.size(); k++) {
int eq_zero = matrics[0][k];
if(eq_zero!=0){
int sign = (k % 2 == 0) ? 1 : -1;
expresult += sign * eq_zero * expansionner(cofacterer(matrics, k));
}
}
return expresult;
}
}
void cofactor_expansion(){
cout << "what is the size of matrics?" << endl;
int size_of_matrics; cin >> size_of_matrics;
vector<vector<int>> matrics(size_of_matrics,vector<int>(size_of_matrics,-1));
cout << "input the elements of the matrics" << endl;
for (int i = 0; i < size_of_matrics; ++i) {
string line; cin >> line;
vector<string> result_of_split = splitByComma(line);
for (int j = 0; j < size_of_matrics; ++j) {
int num = stoi(result_of_split[j]);
matrics[i][j] = num;
}
}
cout << "-------" <<endl;
cout << expansionner(matrics)<<endl;
}
int main() {
cofactor_expansion();
}