题解 | #井字棋#
井字棋
https://www.nowcoder.com/practice/0375c1d206ef48a3894f72aa07b2fdde
#include <stdio.h> #include <stdbool.h> #define ROW 3 #define COL 3 int main() { char arr[ROW][COL] = {0}; bool flag = false; // 输入棋子 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { scanf("%c", *(arr + i) + j); // 吸收换行符 getchar(); } } // 对角线 if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && 'O' != arr[0][0]) { if ('K' == arr[1][1]) { flag = true; printf("KiKi wins!\n"); } else { flag = true; printf("BoBo wins!\n"); } } // 对角线 else if (arr[1][1] == arr[0][2] && arr[0][2] == arr[2][0] && 'O' != arr[1][1]) { if ('K' == arr[1][1]) { flag = true; printf("KiKi wins!\n"); } else { flag = true; printf("BoBo wins!\n"); } } for (int i = 0; i < ROW; i++) { // 行相等 if (arr[i][0] == arr[i][1] && arr[i][2] == arr[i][1] && 'O' != arr[i][0]) { if ('K' == arr[i][0]) { flag = true; printf("KiKi wins!\n"); break; } else { flag = true; printf("BoBo wins!\n"); break; } } // 列相等 else if (arr[0][i] == arr[1][i] && arr[2][i] == arr[0][i] && 'O' != arr[0][i]) { if ('K' == arr[0][i]) { flag = true; printf("KiKi wins!\n"); break; } else { flag = true; printf("BoBo wins!\n"); break; } } } if (!flag) { printf("No winner!\n"); } return 0; }