標籤:
#include<stdio.h>#include<stdlib.h>void printMatrix(int **matrix,int columns,int rows,int start){int x=columns-start-1;int y=rows-start-1;int i;for(i=start;i<=x;i++)//先列印一行printf("%d ",matrix[start][i]);if(start<y)//從上往下{for(i=start+1;i<=y;i++)printf("%d ",matrix[i][x]);}if(x>start&&y>start)//從右至左列印{for(i=x-1;i>=start;i--)printf("%d ",matrix[y][i]);}if(x>start&&y>start+1)//從下到上列印{for(i=y-1;i>=start+1;i--)printf("%d ",matrix[i][start]);}}void matrixClock(int **matrix,int columns,int rows){if(matrix==NULL||columns<=0||rows<=0)return;int start=0;while(columns>start*2&&rows>start*2){printMatrix(matrix,columns,rows,start);start++;}}int main(){int n,m;while(scanf("%d %d",&n,&m)!=NULL){int **numbers;int i,j;numbers=(int **)malloc(sizeof(int *)*n);//n rowsfor(i=0;i<n;i++)numbers[i]=(int *)malloc(sizeof(int)*m);//m columnsfor(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&numbers[i][j]);matrixClock(numbers,m,n);}return 0;}
順時針列印矩陣(C語言代碼)