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;
}