codeup27944 星号空心六边形
时空限制 1000ms/128MB
题目描述
输入正整数n,输出星号空心六边形。
输入
一个正整数n
输出
输出星号空心六边形
样例输入
6
样例输出
******
* *
* *
* *
* *
* *
* *
* *
* *
* *
******
代码
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
for (int i=1; i<=n; ++i){
for (int j=1; j<=n-i; ++j) cout<<" ";
if (i==1)
for (int j=1; j<=n+2*i-2; ++j) cout<<"*";
else {
cout<<"*";
for (int j=1; j<=n+2*i-4; ++j) cout<<" ";
cout<<"*";
}
cout<<endl;
}
for (int i=n-1; i>=1; --i){
for (int j=1; j<=n-i; ++j) cout<<" ";
if (i==1)
for (int j=1; j<=n+2*i-2; ++j) cout<<"*";
else {
cout<<"*";
for (int j=1; j<=n+2*i-4; ++j) cout<<" ";
cout<<"*";
}
cout<<endl;
}
return 0;
}
本文介绍了一个使用 C++ 编写的程序,该程序能够根据用户输入的正整数 n,输出由星号组成的空心六边形图案。通过两段循环结构实现了六边形的上半部分和下半部分的绘制。

276

被折叠的 条评论
为什么被折叠?



