题解 | #井字棋#
井字棋
http://www.nowcoder.com/practice/0375c1d206ef48a3894f72aa07b2fdde
#include <stdio.h> int main() { char board[3][3] = { 0 }; char flag = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf(" %c", &board[i][j]); } } //判断输赢 for (int i = 0; i < 3; i++) { //行 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != 'O') { flag = board[i][2]; break; } //列 if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != 'O') { flag = board[2][i]; break; } } //对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != 'O') { flag = board[2][2]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != 'O') { flag = board[2][0]; } if ('K' == flag) { printf("KiKi wins!\n"); } else if ('B' == flag) { printf("BoBo wins!\n"); } else { printf("No winner!\n"); } return 0; }
编程初学者入门训练 文章被收录于专栏
针对编程初学者入门训练130题的代码详解专栏,内附注释方便理解,牛客130题的代码均用C语言实现,方便初学者学习。