兩個乒乓球隊進行比賽,各出三人,甲隊為A,B,C三人,乙隊為X ,Y ,Z三人,已抽簽決定比賽名單,有人向隊員打聽比賽的名單,A說他不和X比, C說他不和X,Z比請編程序找出三隊賽手的名單
這個程序找出滿足條件的比賽名單。
#include <stdio.h>int main() {char teamA[] = {'A', 'B', 'C'};char teamB[] = {'X', 'Y', 'Z'};char result[3];for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {for (int k = 0; k < 3; k++) {if (i != j && i != k && j != k) {if (teamA[i] != 'A' && teamA[k] != 'C' && teamA[k] != 'C') {result[0] = teamB[i];result[1] = teamB[j];result[2] = teamB[k];}}}}}printf("比賽名單:\nA vs %c\nB vs %c\nC vs %c\n", result[0], result[1], result[2]);return 0;
}
代碼說明:
- 遍歷所有可能的比賽名單組合。
- 檢查是否滿足 A 不和 X 比賽,C 不和 X、Z 比賽的條件。
- 輸出滿足條件的比賽名單。