找规律填写N×N方阵。如N=8时, 其方阵为:
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 2 3 3 3 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 3 3 2 1 1 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1
上代码:
1 #include <stdio.h> 2 3 #define N 10 4 5 int matrix[N][N] = {0}; 6 7 int main() 8 { 9 for(int i = 0; i < N; i++) 10 { 11 for(int j = 0; j < N-i; j++) 12 { 13 if(i < j) 14 { 15 matrix[i][j] = i+1; 16 matrix[N-1-j][N-1-i] = i+1; 17 } 18 else 19 { 20 matrix[i][j] = j+1; 21 matrix[N-1-j][N-1-i] = j+1; 22 } 23 } 24 } 25 26 for(int i = 0; i < N; i++) 27 { 28 for(int j = 0; j < N; j++) 29 { 30 printf("%3d", matrix[i][j]); 31 } 32 printf("\n"); 33 } 34 return 0; 35 }
时间: 2024-12-24 11:37:15