问题 1537: [蓝桥杯][算法提高VIP]栅格打印问题(C++)

题目描述
编写一个程序,输入两个整数,作为栅格的高度和宽度,然后用“+”、“-”和“|”这三个字符来打印一个栅格。
输入
输入只有一行,包括两个整数,分别为栅格的高度和宽度。
输出
输出相应的栅格。

样例输入
3  2 
样例输出
+-+-+
| | |
+-+-+
| | |
+-+-+
| | |
+-+-+
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int m,n;
    cin>>m>>n;
    if(m<=0&&n<=0)
        return 0;
    for(int i=1; i<=2*m+1; i++)
    {
        for(int j=1;j<2*n+1;j++)
        {
            if(i%2==1)
            {
                if(j%2==1)
                    cout<<"+";
                else
                    cout<<"-";
            }
            else
            {
                if(j%2==1)
                    cout<<"|";
                else
                    cout<<" ";
            }
        }
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
发布了8 篇原创文章 · 获赞 6 · 访问量 224

猜你喜欢

转载自blog.csdn.net/qq_41681570/article/details/104364991