题解 | #空心正方形图案#
空心正方形图案
https://www.nowcoder.com/practice/72347ee949dc47399186ee183632f303
#include <stdio.h> int main() { int n = 0, i = 0, j = 0; while (scanf("%d", &n) != EOF) { int right = 2 * n - 1; /*每行元素个数*/ for (i = 1; i <= n; i++) /*共n行*/ { if (i >= 2 && i <= n - 1)/*先考虑第二行到n-1行的情况*/ { for (j = 1; j <= right; j++) { if (j == 1 || j == right) printf("*"); else printf(" "); } printf("\n");/*打印完一行换行*/ } else /*考虑第一行和第n行的情况*/ { for (j = 1; j <= right; j++) { if (j % 2 != 0) printf("*"); else printf(" "); } printf("\n"); } } } return 0; }