
题解:用二维数组
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<vector<string>> a(n+1,vector<string>(n+2," "));
for(int j = 2; j < n+2; j++ ){
if(j == n+1){
a[0][j] = "*";
}else{
a[0][j] = "* ";
}
}
for(int j = 0; j < n+1; j++){
if(j == n){
a[1][j] = "*";
}else{
a[1][j] = "* ";
}
}
for(int i = 0; i < n+1; i++){
for(int j = 0; j < n+2; j++){
if(i + j == n || i + j == n + 1){
if(i + j == n + 1 && i < n-1 && i > 2){
a[i][j] = "*";
}else{
a[i][j] = "* ";
}
}
}
}
for(int j = 2; j < n; j++){
if( j == n-1){
a[n][j] = "*";
}else{
a[n][j] = "* ";
}
}
for(int j = 3; j < n+2; j++){
if(j == n+1){
a[n-1][j] = "*";
}else{
a[n-1][j] = "* ";
}
}
for(int i = 0; i < n+1; i++){
for(int j = 0; j < n+2; j++){
cout<<a[i][j];
}
cout<<endl;
}
}