Structured Program I – Print a Chessboard

853 词

题目描述

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.

1
2
3
4
5
6
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#

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.

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;
}