C# 和C和c++ 是一个祖宗吗 (是否同源)
DO IT YOURSELF !
#include <stdio.h> #include <math.h> #define N 10 void spaces(int n) { while (n--) putchar(' '); } void print(int (*matrix)[N+N-1], int n) { int i, j, k, width, rows, cols; rows = cols = (n << 1) - 1; width = (int)log10(n * n) + 2; for (i = 0; i < rows; ++i) { for (j = 0; !matrix[i][j]; spaces(width)) ++j; for (k = j; j < k + n - 1; printf("%-*d", width + 1, matrix[i][j++])) ; for (; j < cols && matrix[i][j]; printf("%-*d", width, matrix[i][j++])) ; putchar('\n'); } } void initialize(int (*matrix)[N+N-1], int n) { int i, j, row, col, k = n, count; for (i = n - 1, j = 0, count = 1; k > 0; k -= 2, ++i, ++j) { row = col = 0; matrix[i][j] = count; for (; col < k - 1; matrix[i+row][j+col++] = count++) ; for (; row < k - 1; matrix[i+row++][j+col] = count++) ; for (; col > 0; matrix[i+row][j+col--] = count++) ; for (; row > 0; matrix[i+row--][j+col] = count++) ; } for (i = n - 2, j = 1, k = 2; k < n + 1; ++k, --i, ++j) { for (count = k, row = col = 0; col < n - 1; matrix[i+row][j+col++] = count++) ; for (; row < n; matrix[i+row++][j+col] = count++) ; } } int main(void) { int matrix[N+N-1][N+N-1] = { 0 }, n; scanf("%d", &n); getchar(); initialize(matrix, n); print(matrix, n); return 0; }