sdnuoj 1010.菱形与星号加强版

本题考查的是对循环的熟练掌握,和一丢丢数学思考
对于上半部分和下半部分要分别输出;

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
typedef long long ll;
using namespace std;
int main()
{
    
    
    int n;
    cin>>n;
    for(int i = 1; i<=n; i++)//循环输出n行的*,相当于输出了一个正序金字塔
    {
    
    
        for(int j = 0; j < n-i; j++ )
        {
    
    
            cout<<" ";
        }
        int h = 2*i-1;
        while(h--)
        {
    
    
            cout<<"*";
        }
        cout<<'\n';
    }
    for(int i = 1; i<=n-1; i++)//再循环输出倒序的金字塔,因为一共是2*n-1行,所以倒金字塔就得注意一些数的问题
    {
    
    
        for(int j = 1; j<=i; j++)
        {
    
    
            cout<<" ";
        }
        int a = 2*n-2*i-1;//很重要,主要是通过2*m-1实现,而m为(n-i),笔算一下就行
        while(a--)
        {
    
    
            cout<<"*";


        }
        cout<<'\n';//记得换行,不然会堆在一起

    }
    return 0;
}

在这里插入图片描述

(字丑是丑了点,但是人就不一定了,滑稽@@)

猜你喜欢

转载自blog.csdn.net/weixin_51216553/article/details/109517856