Careercup - Microsoft面试题 - 5649647234187264

2014-05-10 22:17

题目链接

原题:

A draw method is given, write a function to draw a chess board. The Draw method, draws a square and  has parameters row position, column position and color.

题目:给定一个draw()方法,请画出一个国际象棋盘。函数参数有行、列和颜色。

解法:这个题是想考察什么?没想太明白,先写个出来吧。我完全不会国际象棋,所以查了下到底是“黑白...”还是“白黑...”。根据有信号发亮,没信号黑的常识,我姑且认为0是黑色吧。所以有了以下代码。

代码:

 1 // http://www.careercup.com/question?id=5649647234187264

 2 // i for row, j for column, color represented as integer id.

 3 // 0 for white, 1 for black.

 4 void draw(int i, int j, int color);

 5 

 6 void drawChessBoard() {

 7     int i;

 8     

 9     /*

10         b w b w b w b w

11         w b w b w b w b

12         b w b w b w b w

13         w b w b w b w b

14         b w b w b w b w

15         w b w b w b w b

16         b w b w b w b w

17         w b w b w b w b

18     */

19     

20     for (i = 0; i < 64; ++i) {

21         draw(i >> 3, i & 7, i + 1 & 1);

22     }

23 }

 

你可能感兴趣的:(Microsoft)