1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<vector>
  5 
  6 using namespace std;
  7 
  8 int n,m,k;
  9 vector<vector<int> >a,b,c;//因为普通二维数组不能作为形参传入函数,所以用vector 
 10 
 11 //快读 
 12 template<typename type>
 13 inline void read(type &x)
 14 {
 15     x=0;bool flag(0);char ch=getchar();
 16     while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
 17     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
 18     flag?x=-x:0;
 19 }
 20 
 21 //快写 
 22 template<typename type>
 23 inline void write(type x,bool mode)
 24 {
 25     x<0?x=-x,putchar('-'):0;static short Stack[200],top(0);
 26     do Stack[++top]=x%10,x/=10; while(x);
 27     while(top) putchar(Stack[top--]|48);
 28     mode?puts(""):putchar(' ');
 29 }
 30 
 31 //初始化 
 32 template<typename type>
 33 inline void init(type n,type m,vector<vector<int> >&a)
 34 {
 35     a.resize(n+1);
 36     for(int i=1;i<=n;i++) a[i].resize(m+1);
 37     return;
 38 }
 39 
 40 //矩阵输入 
 41 template<typename type>
 42 inline void matin(type n,type m,vector<vector<int> >&a)
 43 {
 44     init(n,m,a);
 45     for(int i=1;i<=n;i++)
 46         for(int j=1;j<=m;j++)
 47             read(a[i][j]);
 48     return;
 49 }
 50 
 51 //矩阵输出 
 52 template<typename type>
 53 inline void matout(type n,type m,vector<vector<int> >&a)
 54 {
 55     for(int i=1;i<=n;i++)
 56     {
 57         for(int j=1;j<=m;j++) write(a[i][j],0);
 58         puts(" ");
 59     }
 60     puts("");
 61     return;
 62 }
 63 
 64 //矩阵加减法 
 65 template<typename type>
 66 inline void matadd(type n,type m,vector<vector<int> >&a,vector<vector<int> >&b,vector<vector<int> >&c)
 67 {
 68     init(n,m,c);
 69     for(int i=1;i<=n;i++)
 70         for(int j=1;j<=m;j++)
 71             c[i][j]=a[i][j]+b[i][j];//对减法也适用 
 72     return;
 73 }
 74 
 75 //矩阵数乘 
 76 template<typename type>
 77 inline void matscamul(type n,type m,type k,vector<vector<int> >&a,vector<vector<int> >&c)//k为运算数 
 78 {
 79     init(n,m,c);
 80     for(int i=1;i<=n;i++)
 81         for(int j=1;j<=m;j++)
 82             c[i][j]=a[i][j]*k;
 83     return;
 84 }
 85 
 86 //矩阵乘法 
 87 template<typename type>
 88 inline void matmul(type x,type y,type z,vector<vector<int> >&a,vector<vector<int> >&b,vector<vector<int> >&c)//a矩阵:x*y b矩阵:y*z 
 89 {
 90     init(x,z,c);
 91     for(int i=1;i<=x;i++)
 92         for(int j=1;j<=z;j++)//注意顺序! 
 93             for(int k=1;k<=y;k++)
 94                 c[i][j]+=a[i][k]*b[k][j];
 95     return;
 96 }
 97 
 98 //矩阵转置 
 99 template<typename type>
100 inline void mattrans(type n,type m,vector<vector<int> >&a,vector<vector<int> >&c)
101 {
102     init(m,n,c);
103     for(int i=1;i<=n;i++)
104         for(int j=1;j<=m;j++)
105             c[j][i]=a[i][j];
106     return;
107 }
108 
109 //矩阵的迹
110 template<typename type>
111 inline int mattrace(type n,vector<vector<int> >&a)
112 {
113     int res(0);
114     for(int i=1;i<=n;i++) res+=a[i][i];
115     return res;
116 }
117 
118 signed main()
119 {
120     //矩阵加(减)法(c=a+b)
121     cin>>n>>m;
122     matin(n,m,a);
123     matin(n,m,b);
124     matadd(n,m,a,b,c);
125     matout(n,m,c);
126     
127     //矩阵数乘(c=k*a)
128     cin>>n>>m>>k;
129     matin(n,m,a);
130     matscamul(n,m,k,a,c);
131     matout(n,m,c);
132     
133     //矩阵乘法(c=a*b)
134     cin>>n>>m>>k;
135     matin(n,m,a);
136     matin(m,k,b);
137     matmul(n,m,k,a,b,c);
138     matout(n,k,c);
139     
140     //矩阵转置(c=aT)
141     cin>>n>>m;
142     matin(n,m,a);
143     mattrans(n,m,a,c);
144     matout(m,n,c);
145     
146     //矩阵的迹(res=∑i=1,i<=n(a[i][i]))
147     cin>>n;
148     matin(n,n,a);
149     cout<<mattrace(n,a)<<endl;
150     
151     return 0;
152 }

 

相关文章:

  • 2021-07-08
  • 2021-11-28
  • 2021-03-30
  • 2021-07-20
猜你喜欢
  • 2021-11-22
  • 2021-12-03
相关资源
相似解决方案