一个二维数组: int mat[2][2]={{1,2},{3,4} };
正常输出顺序为 1 2 3 4
顺时针输出为: 1 2 4 3
java实现
public class test {
static void printMat(int mat[][],int i,int j){
int count = 1;
int max = i -1;
for (int m = 0; m < j; m++){
System.out.print(mat[0][m]);
}
for (int n = 1; n < i; n++){
System.out.print( mat[n][j-1]);
}
while (max>0){
if (count % 2 != 0){
for (int a = j - 1-1; a>=0; a--){
System.out.print(mat[max][a]);
}
}
else{
for (int b = 0; b <= j - 1-1; b++){
System.out.print( mat[max][b]);
}
}
count++; max--;
}
}
public static void main(String[] args) {
int mat[][]= {{1,2,3},{4,5,6},{7,8,9}};
printMat(mat,3,3);
}
}
C原因语言实现:
#include<stdlib.h>
#include<stdio.h>
void printMat(int mat[3][3],int m,int n){
int count = 1;
int max = m -1;
for (int i = 0; i < n; i++){
printf("%d", mat[0][i]);
}
for (int j = 1; j < m; j++){
printf("%d", mat[j][n-1]);
}
while (max>0){
if (count % 2 != 0){
for (int a = n - 1-1; a>=0; a--){
printf("%d", mat[max][a]);
}
}
else{
for (int b = 0; b <= n - 1-1; b++){
printf("%d", mat[max][b]);
}
}
count++; max--;
}
}
int main(){
int mat[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, {7,8,9} };
printMat(mat, 3, 3);
system("pause");
}