题解 | #金字塔#
金字塔
https://ac.nowcoder.com/acm/problem/22203
1、观察知应输出一个n*2n-1的矩阵;
2、考虑将一行输出划分为三部分,空白部分,带*部分,空白部分;
3、利用对称性即可
#include <bits/stdc++.h>
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
while(cin>>n){
int Max_num = 2*n - 1;
for(int i = 1;i <= n;i++){
int sum = 2*i - 1; //该层*总个数
int tmp = (Max_num - sum)/2;
for(int j = 1;j <= tmp;j++) cout<<" ";
for(int j = tmp+1;j <= (Max_num-tmp);j++) cout<<"*";
for(int j = (Max_num-tmp)+1;j <= Max_num;j++) cout<<" ";
cout<<'\n';
}
}
return 0;
}

