本文共 2359 字,大约阅读时间需要 7 分钟。
#include#include #include #include #include #include #include #include #define N 100using namespace std;template out_type convert(const in_value & t){ stringstream stream; stream< >result;//向result中写入值 return result;}struct Number{ int x, y; static int gcd(int a, int b){ return b==0 ? a : gcd(b, a%b); } Number(){} Number(int xx, int yy=1):x(xx), y(yy){} Number operator /(Number tmp){ return Number(x*tmp.y/(gcd(x*tmp.y, y*tmp.x)), y*tmp.x/gcd(x*tmp.y, y*tmp.x)); } Number operator *(Number tmp){ return Number(x*tmp.x/gcd(x*tmp.x, y*tmp.y), y*tmp.y/gcd(x*tmp.x, y*tmp.y)); } Number operator -(Number tmp){ int lcm = y*tmp.y/gcd(y, tmp.y);//最小公倍数 int xx = lcm/y*x - lcm/tmp.y*tmp.x; if(xx==0) return Number(0, 1); return Number(xx/gcd(xx, lcm), lcm/gcd(xx, lcm)); } bool operator ==(Number tmp){ return x==tmp.x && y==tmp.y; }};ostream& operator<<(ostream& output, const Number &p){ output< < (p.x)+"/"+convert (p.y)<<" ";}struct Determinant{ Number d[N][N]; int n, m; Determinant(int nn, int mm): n(nn), m(mm){} void out_matrix(){ for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j) cout< =1; --j){ if(inverse.d[j][i]==Number(0,1)) continue; Number mul = inverse.d[j][i]/inverse.d[i][i]; for(int k=i; k<=2*n; ++k) inverse.d[j][k] = inverse.d[j][k]-inverse.d[i][k]*mul; } for(int j=i+1; j<=n; ++j){ if(inverse.d[j][i]==Number(0,1)) continue; Number mul = inverse.d[j][i]/inverse.d[i][i]; for(int k=i; k<=2*n; ++k) inverse.d[j][k] = inverse.d[j][k]-inverse.d[i][k]*mul; } } for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) inverse.d[i][j] = inverse.d[i][j+n]; return inverse; } Determinant company_matrix(){ //伴随矩阵 Determinant company(n, m); for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j){ Determinant tmp(n-1, m-1);//余子式 int r=1, c=1; for(int ii=1; ii<=n; ++ii){ c=1; if(i!=ii){ for(int jj=1; jj<=m; ++jj) if(j!=jj){ tmp.d[r][c] = d[ii][jj]; ++c; } ++r; } } company.d[j][i] = tmp.determinantValue();//赋给伴随矩阵相应的位置 if((i+j)&1) company.d[j][i].x = -company.d[j][i].x; } return company; }};int main(){ int n, m; int x, y; cin>>n>>m; Determinant determinant(n, m);// regex patternOne("([a-z]+)\\.([a-z]+)");// smatch pieces_match; for(int i=1; i<=n; ++i){ string str; for(int j=1; j<=n; ++j){ scanf("%d/%d", &x, &y); determinant.d[i][j] = Number(x, y); } } //伴随矩阵 cout<<"伴随矩阵 "<
转载地址:http://qymra.baihongyu.com/