题解
2026-05-29 06:14:40
发布于:浙江
11阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;//输入
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j||n-i+1==j){cout<<'+';}
//判断这个位置是不是对角线或副对角线
//是的话输出+
else cout<<'-';
//否则的话输出-
}
cout<<endl;//输出换行
}
return 0;
//做法1
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;//输入
int l=0,r=n-2;//l为边上的空格,r为中间的空格
for(int i=1;i<=n/2;i++){
//+---+ l=0 r=3
// | +1 -2
// | 都有两个加
//-+-+- l=1 r=1
for(int j=1;j<=l;j++){cout<<"-";}//左测空格
cout<<"+";//输出+
for(int k=1;k<=r;k++){cout<<"-";}//中间空格
cout<<"+";//输出+
for(int p=1;p<=l;p++){cout<<"-";}//右侧空格
l++;//变化
r-=2;//变化
cout<<endl;//换行
}
//为1~n/2行
for(int i=1;i<=n;i++){
if(i==n/2+1){cout<<"+";}//+都在中间,且只有一个
else{cout<<"-";}//否则输出-
}
cout<<endl;
//为n/2+1行
l=n/2-1;
r=1;
//初始化
for(int i=1;i<=n/2;i++){
//-+-+- l=1 r=1
// | -1 +2
// | 都有两个加
//+---+ l=0 r=3
//---+-+--- l=3(=n/2-1,n=9) r=1
// | -1 +2
// | 都有两个加
//--+---+-- l=2 r=3
for(int j=1;j<=l;j++){cout<<"-";}//左侧空格
cout<<"+";
for(int k=1;k<=r;k++){cout<<"-";}//中间空格
cout<<"+";
for(int p=1;p<=l;p++){cout<<"-";}//右侧空格
l--;//变化
r+=2;//变化
cout<<endl;//换行
}
//为n/2+2~n行
return 0;
}
都看到这了,点个赞再走吧~~




这里空空如也





有帮助,赞一个