https://pintia.cn/problem-sets/994805260223102976/problems/994805294251491328
应该有更简单的输出格式控制,以后再更
#include <iostream>
using namespace std;
int main(){
char c;
int n, yuxia, cnt = 1, need = 3, max;
cin >> n >> c;
--n;
while(1){
if(n >= 2*need){
cnt++;
n -= 2*need;
need += 2;
}
else{
yuxia = n;
break;
}
}
max = 2*cnt-1;
int tmp1, tmp2;
//打印上顶
for(int i = 0; i < max/2; i++){
tmp1 = max-2*i; tmp2 = i;
for(int j = 0; j < tmp2; j++)
cout << " ";
for(int j = 0; j < tmp1; j++)
cout << c;
cout << endl;
}
//打印中间
tmp1 = 1, tmp2 = max/2;
for(int j = 0; j < tmp2; j++)
cout << " ";
for(int j = 0; j < tmp1; j++)
cout << c;
cout << endl;
//打印下体
tmp1 = 3;
tmp2 = (max-tmp1)/2;
for(int i = 0; i < max/2; i++){
for(int j = 0; j < tmp2; j++)
cout << " ";
for(int j = 0; j < tmp1; j++)
cout << c;
cout << endl;
tmp1 += 2;
tmp2 = (max-tmp1)/2;
}
cout << yuxia;
return 0;
}
本文介绍了一个使用C++实现的字符金字塔绘制算法。该算法通过输入指定的高度和字符,能够绘制出由该字符组成的金字塔形状。文章详细展示了如何通过循环结构控制字符的输出,形成金字塔的上部、中部和下部,并提供了完整的源代码。
1285

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



