// test35.cpp : Defines the entry point for the console application. //顺时针打印矩阵 #include "stdafx.h" #include "stdio.h" void _print1(int a[5][5],int start_x,int end_x,int start_y){ while(start_x<=end_x){ printf("%d ",a[start_x++][start_y]); } } void _print2(int a[5][5],int end_x, int start_y,int end_y){ int temp=start_y+1; while(temp<=end_x){ printf("%d ",a[end_x][temp++]); } } void _print3(int a[5][5],int end_x,int start_x,int end_y){ int temp=end_x-1; while(temp>=start_x) printf("%d ",a[temp--][end_y]); } void _print4(int a[5][5],int start_x,int end_y,int start_y){ int temp=end_y-1; while(temp>=start_y+1) printf("%d ",a[start_x][temp--]); } void _print(int a[5][5],int colunm,int row,int start_x,int start_y){ int end_x=row-1-start_x; int end_y=row-1-start_y; _print1(a, start_x, end_y, start_y); _print2(a, end_x, start_y, end_y); _print3(a, end_x, start_x, end_y); _print4(a, start_x, end_y, start_y); } void print(int a[5][5],int colunm,int row){ if( !a || !colunm || !row) return; int start_x=0; int start_y=0; while(start_x*2<colunm && start_y*2<row){ _print(a,colunm,row,start_x,start_y); ++start_x; ++start_y; } } void main(){ int a[5][5]; for(int i=0;i<5;i++) for(int j=0;j<5;j++) a[i][j]=i+j; print(a,5,5); }