CCF NOI1032. 菱形 (C++)

版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/84935593

1032. 菱形

题目描述

输入一个正整数n,输出用1至(2n-1)的数字组成的菱形。

输入

输入正整数n。

输出

输出对应的菱形(见样例)。

样例输入

3

样例输出

1
123
12345
123
1

数据范围限制

1<=n<=10

C++代码

#include <iostream>
#include <cassert>

using namespace std;

int main()
{
    int n;
    cin >> n;

    assert(1<=n && n<=10);

    for(int row=1; row<=n; row++)
    {
        int i=1;
        for(int col=1; col<=n+row-1; col++)
        {
            if(col < n-row+1)
            {
                cout << " ";
            }
            else
            {
                cout << i++;
            }
        }
        cout << endl;
    }

    for(int row=n-1; row>=1; row--)
    {
        int i=1;
        for(int col=1; col<=n+row-1; col++)
        {
            if(col < n-row+1)
            {
                cout << " ";
            }
            else
            {
                cout << i++;
            }
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43379056/article/details/84935593