算法提高 栅格打印问题

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/82821914

问题描述

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

样例输入

3 2

样例输出

+-+-+
| | |
+-+-+
| | |
+-+-+
| | |
+-+-+

#include<iostream>
using namespace std;
int main()
{
    int m, n;
    
    cin >> m >> n;
    
    for(int i=0; i<2*m+1; i++){
        if(m==0 || n==0){
            break;
        }
        if(i%2 == 0){
            cout<<"+";
            for(int j=0; j<n; j++){
                cout << "-+";
            }
            cout << endl;
        }
        else{
            cout << "|";
            for(int j=0; j<n; j++){
                cout << " |";
            }
            cout << endl;
        }
    
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/82821914