用C⼀C++如何实现矩阵的幂运算,求高手作答~

2024-12-27 00:36:08
推荐回答(4个)
回答1:

#include
#include
#include
#include
int ** alloc2d(int m) {
int **array,i;
array=(int **)malloc(m*sizeof(int *));
if(array==NULL) return NULL;
for(i=0;i array[i]=(int *)malloc(m*sizeof(int));
if(array[i]==NULL) {for(;i>=0;i--) {free(array[i]);} free(array);return NULL;}
}
return array;
}
void autoinput(int **a,int m) {
int i,j;
srand(time(0));
for(i=0;i for(j=0;j *(*(a+i)+j)=rand()%3;
}
void out(int **a,int m) {
int i,j;
for(i=0;i for(j=0;j printf("%4d",*(*(a+i)+j));
printf("\n");
}
}
int **mltply(int **a,int **b,int **c,int m) {
int i,j,k;
for(i=0;i for(j=0;j for(*(*(c+i)+j)=0,k=0;k *(*(c+i)+j)+=*(*(a+i)+k)*(*(*(b+k)+j));
return c;
}
int main() {
int i,m,times,j,n=0;
int **ops[3];
printf("input乘法方阵规模m*m几次幂:\n");
scanf("%d%d",&m,×);
for(i=0;i<3;i++) {
ops[i]=alloc2d(m);
if(i<=0) {autoinput(ops[i],m);printf("矩阵%d\n",i+1);out(ops[i],m);printf("\n");}
}
for(int h=1;h<=2;h++)
for(i=0;i for(j=0;j *(*(ops[h]+i)+j)=0;
printf("\n乘法\n");
mltply(ops[0],ops[0],ops[1],m);
times-=2;
while(times) {
if(n++%2==0) mltply(ops[1],ops[0],ops[2],m);
else mltply(ops[2],ops[0],ops[1],m);
--times;
}
if(n%2==0) out(ops[1],m);
else out(ops[2],m);
return 0;
}
//矩阵输入用的随机
//可能麻烦了点 要不你优化一下

回答2:

每次用中间变量存运算结果,然后再赋值回去,
求b[k][k]的T次方,res初始是单位矩阵,c用来存中间结果
while(T){//快速幂模板
if(T&1){//if(A&1) res*=A
for(i=1;i<=k;i++)
for(j=1;j<=k;j++){
c[i][j]=0;
for(h=1;h<=k;h++)
if(res[i][h]&&b[h][j])
c[i][j]+=res[i][h]*b[h][j];
}
for(i=1;i<=k;i++)
for(j=1;j<=k;j++)
res[i][j]=c[i][j]%10000;
}
for(i=1;i<=k;i++)//A*A
for(j=1;j<=k;j++){
c[i][j]=0;
for(h=1;h<=k;h++)
if(b[i][h]&&b[h][j])
c[i][j]+=b[i][h]*b[h][j];
}
for(i=1;i<=k;i++)
for(j=1;j<=k;j++)
b[i][j]=c[i][j]%10000;
T=T>>1;
}

回答3:

#include "stdio.h"
#include
#include
#include
#include
#define size 100
typedef float datatype[size][size];

typedef struct arry{
datatype arrys;
int m,n;
}arry;

void creat(arry &x){
printf("\t\tinput arry m and n ( <=100 ): ");
scanf("%d%d",&x.m,&x.n);
printf("input arry %d x %d (data type : float ):\n",x.m,x.n);
int i,j;
for(i=0;i printf("%-3d",i+1);
printf("\n");
for(i=0;i for(j=0;j scanf("%f",&x.arrys[i][j]);
}
}
}
void print(arry x){
int i,j;
printf("\tARRY m= %d n= %d\n",x.m,x.n);
printf("\n");
for(i=0;i for(j=0;j printf("\t%.2f ",x.arrys[i][j]);
}
printf("\n");
}
}

float ixj(arry x,int i,arry &y,int j){
float s=0;
for(int k=0;k s+=x.arrys[i][k]*y.arrys[k][j];
}
return s;
}

arry AXB(arry &x,arry &y){
int i,j;
arry z=x;
for(i=0;i for(j=0;j z.arrys[i][j]=ixj(x,i,y,j);
}
}
return z;
}

arry A_N(arry &x,int expn){
arry y;
for(int i=0;i {
if(i==0)
y=x;
else
y=AXB(x,y);
}
return y;
}
void main(){
arry x,y;
int expn,n=1;
while(n){
printf("***********************ARRY EXPN*************************\n");
printf("\n\t\t1:input a arry\n\t\t2:output arry(A)");
printf("\n\t\t3:Evaluate A^n\n\t\t0:end\n");
printf("\t\tyour order: ");
scanf("%d",&n);
switch(n){
case 1:creat(x);break;
case 2: print(x);break;
case 3: printf("\t\tinput arry A^N N (>=1): ");
scanf("%d",&expn);
y=A_N(x,expn); print(y); break;
case 0:break;
}
printf("\n\t\tenter any press is continue!");
getch();
system("cls");
}
}

回答4:

#include
#include

void Output(int size, int * screwSquare);
void InitScrewSquare(int size, int * screwSquare);
void TransposeSquare(int size, int * screwSquare);
void CalculateDiagonal(int size, int * screwSquare);

int main()
{
int size;
cout << "Input the Size of the ScrewSquare" ;
cin >> size; //矩阵的大小,即n*n的n值
int * screwSquare = new int[size * size];
memset(screwSquare, 0, sizeof(int)*size*size);
InitScrewSquare(size, screwSquare);
Output(size, screwSquare);
TransposeSquare(size, screwSquare);
cout << "Transpose the Square" << endl;
Output(size, screwSquare);
CalculateDiagonal(size, screwSquare);
return 0;
}

void Output(int size, int * screwSquare)
{
int i, j;
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
cout << screwSquare[i*size + j] << '\t';
}
cout << endl;
}
}

void InitScrewSquare(int size, int *screwSquare)
{
int max = size*size;
int row, col, direction;
row = 0, col = 0, direction = 0;
int account;
for(account = 1; account <= max; account++)
{
screwSquare[row * size + col] = account;
switch(direction)
{
case 0:
if(row > size || screwSquare[(row + 1) * size + col] > 0)
{
direction++;
direction%=4;
col++;
}
else
{
row++;
}
break;
case 1:
if(col > size || screwSquare[row * size + col + 1] > 0)
{
direction++;
direction%=4;
row--;
}
else
{
col++;
}
break;
case 2:
if(row < 0 || screwSquare[(row - 1) * size + col] > 0)
{
direction++;
direction %= 4;
col--;
}
else
{
row--;
}
break;
case 3:
if(col < 0 || screwSquare[row * size + col - 1] > 0)
{
direction++;
direction %= 4;
row++;
}
else
{
col--;
}
break;
default:
break;
}
}
}

void TransposeSquare(int size, int *screwSquare)
{
int i, j, temp;
for(i = 0; i < size; i++)
{
for(j = 0; j < i; j++)
{
temp = screwSquare[i*size + j];
screwSquare[i * size + j] = screwSquare[j * size + i];
screwSquare[j * size + i] = temp;
}
}
}

void CalculateDiagonal(int size, int * screwSquare)
{
int sum1, sum2;
sum1 = 0, sum2 = 0;
int i;
for(i = 0; i < size; i++)
{
sum1 += screwSquare[i*size + i];
sum2 += screwSquare[i*size + (size - i - 1)];
}
cout << "The Sum of ScrewSquare Is " << sum1 << "\t"<< sum2 << endl;
}