矩阵快速乘法---代码

it2022-05-05  147

矩阵乘法的核心代码:

while(N) { if(N&1) res=res*A; n>>=1; A=A*A; } 然后是示例代码:

#include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> using namespace std; int N; struct matrix { int a[3][3]; }origin,res; matrix multiply(matrix x,matrix y) { matrix temp; memset(temp.a,0,sizeof(temp.a)); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { temp.a[i][j]+=x.a[i][k]*y.a[k][j]; } } } return temp; } void init() { printf("随机数组如下:\n"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { origin.a[i][j]=rand(); printf("
转载请注明原文地址: https://win8.8miu.com/read-1416.html

最新回复(0)