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.

画一个高度为H cm,宽度为W cm的棋盘。 例如,下图显示了一个高度为6厘米,宽度为10厘米的棋盘。

        例如:5 6 

            #.#.#.
            .#.#.#
           #.#.#.
            .#.#.#
            #.#.#.
代码解释
 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int h,w;
 6     cin >> h >> w;
 7     while(h!=0)
 8     {
 9         int i,j;
10         for(i=0;i<h;i++)
11         {
12             for(j=0;j<w;j++)
13             {
14                 if((i%2==0&&j%2==0||i%2==1&&j%2==1))//如果i和j除以2的余数均为0或者i和j除以2的余数均为1,输出#,否则输出.
15                 cout << "#";
16                 else cout << ".";
17             }
18             cout << endl;
19         }
20         cout << endl;
21         cin >> h >> w;
22     }
23     return 0;
24 }

 

posted @ 2019-08-05 11:22  Accept_program  阅读(238)  评论(0编辑  收藏  举报