问题描述
c语言实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
using namespace std;int main() {int n; //存放外框尺寸int a,b; //存放两个字符char matrix[100][100] = { 0 };//全部初始化为休止符'/0'while (scanf("%d %c %c", &n, &a, &b) != EOF) {int cur = a;//存放当前填充字符memset(matrix, 0, 10000); for (int i = 0; i <= n / 2; i++){//左上:n/2-i , n/2-i//右上:n/2-i , n/2+i//左下:n/2+i , n/2-i//右下:n/2+i , n/2+i//左上到右上for (int x = n / 2 - i, y = n / 2 - i; y <= n / 2 + i; y++) {matrix[x][y] = cur;}//左下到右下for (int x = n / 2 + i, y = n / 2 - i; y <= n / 2 + i; y++) {matrix[x][y] = cur;}//左上到左下for (int x = n / 2 - i, y = n / 2 - i; x <= n / 2 + i; x++) {matrix[x][y] = cur;}//右上到右下for (int x = n / 2 - i, y = n / 2 + i; x <= n / 2 + i; x++) {matrix[x][y] = cur;}if (a == cur) {cur = b;}else {cur = a;}}if (n != 1) {matrix[0][0] = ' ';matrix[0][n - 1] = ' ';matrix[n - 1][n - 1] = ' ';matrix[n - 1][0] = ' ';}for (int i = 0; i < n; ++i) {printf("%s\n", matrix[i]);}}return 0;
}