C语言实现打印空心正方形
思路:观察图中空心正方形,可知首行列和尾行列被黑色外框包裹,其它均为空。所以按观察打印即可。
总代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>int main()
{int n = 0;while (scanf("%d", &n) != EOF){for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (i == 0 || j == 0 || i == n - 1 || j == n - 1){printf("* ");//保持美感加入空格}else{printf(" ");//双空格}}printf("\n");//打印完一行换行}}return 0;
}