题解 | #金字塔#

金字塔

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;
}
全部评论

相关推荐

牛至超人:您好,京东物流岗了解一下吗?负责精加工食品的端到端传输
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务