分析:
从i到n代表的是从最里面一圈的1到最外部一圈的n,每圈的第一行其实比下一圈要多2.
j用来判断奇数偶数圈,且用于上图每次 起始点的计算,每次按箭头与中心多跑离比上次多一个单位距离,
#include<iostream>
using namespace std;
int main(){
char matrix[85][85];
char a,b;
int n;
bool f = true;
while(scanf("%d %c %c",&n,&a,&b)!=EOF){
for(int i = 1,j = 1;i <= n; i += 2,j++){
int x = n / 2 +1,y = x;
x -= j-1;y -= j-1;
char c = j % 2 == 1 ? a : b;
for(int k = 1; k <= i; k++){
matrix[x+k-1][y] = c;
matrix[x][y+k-1] = c;
matrix[x+i-1][y+k-1] = c;
matrix[x+k-1][y+i-1] = c;
}
}
if(n!=1){
matrix[1][1] = ' ';
matrix[1][n] = ' ';
matrix[n][1] = ' ';
matrix[n][n] = ' ';
}
for(int i = 1;i <= n; i++){
for(int j = 1;j <= n;j++){
printf("%c",matrix[i][j]);
}
cout<<endl;
}
}
}