#include <bits/stdc++.h> using namespace std; int main() { int a1 = 0, b1 = 0, c1 = 0; int a2 = 0, b2 = 0, c2 = 0; int x1 = 0, x2 = 0; int y1 = 0, y2 = 0; int z1 = 0, z2 = 0; int ret1 = 0, ret2 = 0, ret3 = 0, ret4 = 0; scanf("%d %d %d",&a1,&b1,&c1); scanf("%d %d %d",&a2,&b2,&c2); scanf("%d %d",&x1,&x2); scanf("%d %d",&y1,&y2); scanf("%d %d",&z1,&z2); ret1 = a1*x1+b1*y1+c1*z1; ret2 = a1*x2+b1*y2+c1*z2; ret3 = a2*x1+b2*y1+c2*z1; ret4 = a2*x2+b2*y2+c2*z2; printf("%d %d \n",ret1,ret2); printf("%d %d \n",ret3,ret4); return 0; }
#include<stdio.h> int main() { int a[2][3],b[3][2],c[2][2]={0},i,j,k; //输入两个数组 for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); //两个数组相乘 for(i=0; i<2; i++)//ik作为数组a//kj作为数组b//ij作为数组c for(j=0; j<2; j++) for(k=0; k<3; k++) c[i][j]+=a[i][k]*b[k][j]; //输出 for(i=0;i<2;i++) { for(j=0;j<2;j++) printf("%d ",c[i][j]); printf("\n"); } }
import java.util.Scanner; public class Main { public static final int n=2; public static final int m=3; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] matrix = new int[n][m]; int[][] matrix1 = new int[m][n]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j]=scanner.nextInt(); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) matrix1[i][j]=scanner.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int sum=0; for (int k = 0; k < m; k++) sum+=matrix[i][k]*matrix1[k][j]; System.out.print(sum+" "); } System.out.println(); } } }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<map> #include<cmath> using namespace std; int main() { int a[2][3], b[3][2], c[2][2] = {0}; for (int i = 0; i < 2;i++) { for (int j = 0; j < 3;j++) { cin >> a[i][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cin >> b[i][j]; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3;k++) { c[i][j] += a[i][k] * b[k][j]; } cout << c[i][j] << " "; } cout << endl; } return 0; }
while True: try: arr1=[] arr2=[] for i in range(2): arr1.append(list(map(int,input().strip().split(' ')))) for i in range(3): arr2.append(list(map(int,input().strip().split(' ')))) result=[] for i in range(2): result.append([0]*2) for i in range(2): for j in range(2): sum1=0 for k in range(3): sum1+=arr1[i][k]*arr2[k][j] result[i][j]=str(sum1) for i in result: print(' '.join(i)+' ') except: break 坑在于每个数字后面都要空格
#include <stdio.h> int main() { int a[2][3], b[3][2], c[2][2]; while(scanf("%d", &a[0][0])!=EOF) {//数组实在太小,我都不想写循环 scanf("%d %d", &a[0][1], &a[0][2]); scanf("%d %d %d", &a[1][0], &a[1][1], &a[1][2]); scanf("%d %d", &b[0][0], &b[0][1]); scanf("%d %d", &b[1][0], &b[1][1]); scanf("%d %d", &b[2][0], &b[2][1]); for(int i=0; i<2; i++){//算了,向循环低头 for(int j=0; j<2; j++){ c[i][j]=0; } } for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ for(int k=0; k<3; k++) { c[i][j]+=a[i][k]*b[k][j]; } } } //就不用循环 printf("%d %d \n%d %d \n", c[0][0], c[0][1], c[1][0], c[1][1]); } return 0;//完事了 }
#include<stdio.h> int main (){//the shorter,the better. int m[12]; for (;~scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&m[0],&m[1],&m[2],&m[3],&m[4],&m[5],&m[6],&m[7],&m[8],&m[9],&m[10],&m[11])&&printf("%d %d\n%d %d\n",m[0]*m[6]+m[1]*m[8]+m[2]*m[10],m[0]*m[7]+m[1]*m[9]+m[2]*m[11],m[3]*m[6]+m[4]*m[8]+m[5]*m[10],m[3]*m[7]+m[4]*m[9]+m[5]*m[11]);); }
#include<stdio.h> int main(){ int a[2][3],b[3][2]; scanf("%d%d%d%d%d%d",&a[0][0],&a[0][1],&a[0][2],&a[1][0],&a[1][1],&a[1][2]); scanf("%d%d%d%d%d%d",&b[0][0],&b[0][1],&b[1][0],&b[1][1],&b[2][0],&b[2][1]); printf("%d ",b[0][0]*a[0][0]+b[1][0]*a[0][1]+b[2][0]*a[0][2]); printf("%d \n",b[0][1]*a[0][0]+b[1][1]*a[0][1]+b[2][1]*a[0][2]); printf("%d ",b[0][0]*a[1][0]+b[1][0]*a[1][1]+b[2][0]*a[1][2]); printf("%d \n",b[0][1]*a[1][0]+b[1][1]*a[1][1]+b[2][1]*a[1][2]); return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ final int I = 2; final int J = 3; Scanner sc = new Scanner(System.in); int[][] a = new int[I][J]; int[][] b = new int[J][I]; int[][] c = new int[I][I]; while(sc.hasNext()){ for(int i=0;i<I;i++){ for(int j=0;j<J;j++){ a[i][j] = sc.nextInt(); } } for(int i=0;i<J;i++){ for(int j=0;j<I;j++){ b[i][j] = sc.nextInt(); } } for(int i=0;i<I;i++){ for(int j=0;j<I;j++){ c[i][j]=0; for(int k=0;k < J;k++ ){ c[i][j]=a[i][k]*b[k][j]+c[i][j]; } } } for(int i=0;i<I;i++){ for(int j=0;j<I;j++){ System.out.print(c[i][j]+" "); if(j==I-1){ System.out.println(""); } } } } } }本代码并没有写死,测试了一下,修改final修饰的值可使用于其他n*m矩阵,就是循环多了点,哈哈,没办法,想到改良版再来看看先,思路通用的,其他语言也可以实现
//太久没做矩阵乘法了,所以算法写的很不熟 #include<iostream> using namespace std; int main() { int a[2][3], b[3][2], c[2][2] = { 0 }; for (int i = 0; i<2; i++) for (int j = 0; j<3; j++) cin >> a[i][j]; for (int i = 0; i<3; i++) for (int j = 0; j<2; j++) cin >> b[i][j]; for (int i = 0; i<2; i++) for (int j = 0; j<2; j++) for (int k = 0; k<3; k++) c[i][j] += a[i][k] * b[k][j]; for(int i=0;i<2;i++){ for(int j=0;j<2;j++) cout<<c[i][j]<<" "; cout<<endl; } return 0; }
#include <cstdio> int main(){ int a11,a12,a13,a21,a22,a23; int b11,b12,b21,b22,b31,b32; int c11,c12,c21,c22; scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &a11,&a12,&a13,&a21,&a22,&a23, &b11,&b12,&b21,&b22,&b31,&b32); c11 = a11*b11 + a12*b21 + a13*b31; c12 = a11*b12 + a12*b22 + a13*b32; c21 = a21*b11 + a22*b21 + a23*b31; c22 = a21*b12 + a22*b22 + a23*b32; printf("%d %d\n%d %d\n",c11,c12,c21,c22); return 0; }
#include<bits/stdc++.h>
int main(){
int a[2][3],b[3][2],c[2][2]={0};
while(scanf("%d %d %d %d %d %d",&a[0][0],&a[0][1],&a[0][2],&a[1][0],&a[1][1],&a[1][2])!=EOF){
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(int i=0;i<3;i++){
c[0][0]+=a[0][i]*b[i][0];
c[0][1]+=a[0][i]*b[i][1];
c[1][0]+=a[1][i]*b[i][0];
c[1][1]+=a[1][i]*b[i][1];
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
}
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; struct Matrix { int matrix[3][3]; int row, col; //行、列 Matrix(int r, int c): row(r), col(c) {} //构造函数 }; Matrix Multiply(Matrix x, Matrix y); void PrintMatrix(Matrix x); //矩阵乘积 int main() { Matrix x(2, 3); Matrix y(3, 2); //输入x矩阵 for (int i = 0; i < x.row; i++) { for (int j = 0; j < x.col; j++) { scanf("%d", &x.matrix[i][j]); // cin >> x.matrix[i][j] ; } } //输入y矩阵 for (int i = 0; i < y.row; i++) { for (int j = 0; j < y.col; j++) { scanf("%d", &y.matrix[i][j]); // cin >> y.matrix[i][j] ; } } //返回相乘后的矩阵 Matrix answer = Multiply(x, y); //打印 PrintMatrix(answer); return 0; } //矩阵乘法 Matrix Multiply(Matrix x, Matrix y) { Matrix answer(x.row, y.col); //前两个for用于返回 Matrix 赋值 for (int i = 0; i < answer.row; i++) { for (int j = 0; j < answer.col; j++) { answer.matrix[i][j] = 0; for (int k = 0; k < x.col; k++) { answer.matrix[i][j] += x.matrix[i][k] * y.matrix[k][j]; } } } return answer; } //输出矩阵 void PrintMatrix(Matrix x) { for (int i = 0; i < x.row; i++) { for (int j = 0; j < x.col; j++) { cout << x.matrix[i][j] << ' '; } cout << endl; } }
#include<iostream> #include<vector> using namespace std; int main() { vector< vector<int> > A(2,vector<int>(3)); vector< vector<int> > B(3,vector<int>(2)); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { int temp; cin >> temp; A[i][j] = temp; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { int temp; cin >> temp; B[i][j] = temp; } } vector<vector<int> > C(2,vector<int>(2)); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { C[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + A[i][2] * B[2][j]; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout<<C[i][j]<<" "; } cout<<endl; } return 0; }
#include <array> #include <iostream> #include <istream> #include <ostream> using namespace std; const int MAXSIZE = 3; //矩阵最大规模(行数或列数) struct Matrix { array<array<int, MAXSIZE>, MAXSIZE>matrix; int rows, cols; //行数和列数 Matrix(int r, int c): rows(r), cols(c) {} //构造函数 //重载输入流运算符 friend istream& operator>>(istream& in, Matrix& m); //重载输出流运算符 friend ostream& operator<<(ostream& out, const Matrix& m); //重载乘法运算符 friend Matrix operator*(const Matrix& x, const Matrix& y); }; //重载输入流运算符 istream& operator>>(istream& in, Matrix& m) { for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { in >> m.matrix[i][j]; } } return in; } //重载输出流运算符 ostream& operator<<(ostream& out, const Matrix& m) { for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { out << m.matrix[i][j]; j == m.cols - 1 ? out << endl : out << " "; } } return out; } //重载乘法运算符 Matrix operator*(const Matrix& x, const Matrix& y) { Matrix result(x.rows, y.cols); for (int i = 0; i < result.rows; i++) { for (int j = 0; j < result.cols; j++) { result.matrix[i][j] = 0; for (int k = 0; k < x.cols; k++) { result.matrix[i][j] += x.matrix[i][k] * y.matrix[k][j]; } } } return result; } int main() { Matrix x(2, 3), y(3, 2); cin >> x >> y; cout << x * y; return 0; }
#include <stdio.h> int main() { int a[2][3]; int b[3][2]; int c[2][2]; int i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); for(i=0;i<2;i++){ for(j=0;j<2;j++){ c[i][j] = a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j]; printf("%d ",c[i][j]); } printf("\n"); } return 0; }