Structured Program I – Print a Chessboard

HZK‘s Blog
本文标题:Structured Program I – Print a Chessboard
本文链接地址:https://blog.zekun.fun/2020/coding/cpp@structured-program-i-print-a-chessboard/

题目描述

Print a Chessboard
Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm.

#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#

Note that the top left corner should be drawn by ‘#’.

输入

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.
The input ends with two 0 (when both H and W are zero).

输出

For each dataset, print the chessboard made of ‘#’ and ‘.’.
Print a blank line after each dataset.

#include<iostream>
using namespace std;
int main() {
    int h, w;
    cin >> h >> w;
    while (h != 0) {
        int i, j;
        for (i = 0; i < h; i++) {
            for (j = 0; j < w; j++) {
                if ((i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1))cout << "#";
                else cout << ".";
            }
            cout << endl;
        }
        cout << endl;
        cin >> h >> w;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值