c语言编写函数旋转矩阵 顺时针旋转矩阵c语言

用C语言编写一个矩阵转置的函数,矩阵的行数和列数在程序中由用户输入,请问怎么写,非常感谢

我的代码逻辑是:

创新互联公司专注于平邑网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供平邑营销型网站建设,平邑网站制作、平邑网页设计、平邑网站官网定制、重庆小程序开发公司服务,打造平邑网络公司原创品牌,更为您提供平邑网站排名全网营销落地服务。

矩阵行指针初值指向每行首地址,迭代依次取所有行指针指向值组成新行,所有行指针自增。最终组合新的矩阵。

#include stdio.h

#include malloc.h

int **getList(int row,int clo);//获取矩阵地址空间

void setNum(int **nList,int n);//填写数值

void prtList(int **nList,int row,int clo);//打印矩阵

int **zz(int **nList,int row,int clo);//转置函数

int main()

{

int row,clo,**nList=NULL,**nListSave=NULL;

printf("输入矩阵行列数:");

scanf("%d%d",row,clo);

nList=getList(row,clo);

setNum(nList,row*clo);

printf("输入的矩阵为:\n");

prtList(nList,row,clo);

printf("转置后的矩阵为:\n");

nListSave=zz(nList,row,clo);

free(nList);

nList=nListSave;

prtList(nList,clo,row);

return 0;

}

int **zz(int **nList,int row,int clo)

{

int *nSave=NULL,**listSave=NULL,**listp=nList,*p=NULL,i,j;

nSave=(int *)malloc(sizeof(int)*row*clo);

listSave=(int **)malloc(sizeof(int*)*clo);//倒置后的矩阵

p=nSave;

for(j=0;jclo;j++)

{

for(i=0;irow;i++)

{

*p++=*listp[i];

listp[i]=listp[i]+1;

}

}

for(i=0;iclo;i++)

listSave[i]=nSave[i*row];

for(i=0;irow;i++)

free(nList[i]);//释放原矩阵行空间

return  listSave;

}

void prtList(int **nList,int row,int clo)

{

int i,j;

for(i=0;irow;i++)

{

for(j=0;jclo;j++)

printf("%d ",nList[i][j]);

printf("\n");

}

}

void setNum(int **nList,int n)

{

int *p=nList[0];

printf("填写矩阵中%d个数值:\n",n);

while(n--0)

scanf("%d",p++);

}

int **getList(int row,int clo)

{

int *nums,**nList,i;

nums=(int *)malloc(sizeof(int)*row*clo);

nList=(int **)malloc(sizeof(int*)*row);

for(i=0;irow;i++)

nList[i]=nums[i*clo];

return nList;

}

旋转矩阵 C语言

试试吧 这个是我自己在网吧写的

#include stdio.h

#include malloc.h

void main(){

int m;

int n;

char *p;

scanf("%d",m);

scanf("%d",n);

p=(int *)malloc(sizeof(char)*m*n);

for(int i=0;im*n;i++)

p[i]='\0';

int x=0,y=0;

int state=0;

for(int i=0;im*n;i++){

p[x*m+y]='A'+(char)(i%26);

switch(state){

case 0:

y++;

if(y=n||p[x*n+y]!='\0'){

state=1;

y--;

x++;

}

break;

case 1:

x++;

if(x=n||p[x*n+y]!='\0'){

state=2;

x--;

y++;

}

break;

case 2:

y--;

if(y0||p[x*n+y]!='\0'){

state=3;

y++;

x--;

}

break;

case 3:

x--;

if(x0||p[x*n+y]!='\0'){

state=0;

x++;

y++;

}

break;

}

}

for(int i=0;im*n;i++){

printf("%c",p[i]);

if(i%n==n-1){

printf("\n");

}

}

}

c语言 旋转矩阵算法

#include stdlib.h

#include stdio.h

#define N 5

int min(int a,int b,int c,int d)

{

a=ab?a:b;

a=ac?a:c;

a=ad?a:d;

return a;

}

int main()

{

int arr[N][N],i,j;

for(i=0;iN;i++)

{

for(j=0;jN;j++)

{

arr[i][j]=min(i,j,N-1-i,N-1-j);

printf("%d ",arr[i][j]);

}

printf("\n");

}

system("pause");

return 0;

}

C语言如何编写旋转矩阵源代码

#include stdio.h

void main()

{

int M;

int N;

int flag=0;

int num1=0;

int num2=1;

int i=0;

int j=0;

char str='A';

char Array[31][31];

printf("请输入矩阵的行M和矩阵的列N:");

scanf("%d%d",M,N);

Array[0][0]=str;

while(flag M*N-1)

{

switch(num2%4)

{

case 1:

++j;

if(j == N-num1-1)

{

num2++;

}

break;

case 2:

++i;

if(i == M-num1-1)

{

num2++;

}

break;

case 3:

--j;

if(j == num1)

{

num2++;

}

break;

case 0:

--i;

if(i == num1+1)

{

num2++;

num1++;

}

break;

}

if(++str == 'Z'+1)

str='A';

Array[i][j]=str;

flag++;

}

for(i=0;iM;i++)

{

for(j=0;jN;j++)

{

printf("%c ",Array[i][j]);

}

printf("\n");

}

}

这样可以么?


当前名称:c语言编写函数旋转矩阵 顺时针旋转矩阵c语言
本文网址:http://myzitong.com/article/higiid.html