YouTip LogoYouTip

C Exercise Example22

# C Exercise Example 22 [![Image 3: C Classic 100 Examples](#) C Classic 100 Examples](#) **Problem:** Two ping-pong teams are competing, each fielding three players. Team A consists of players a, b, and c. Team B consists of players x, y, and z. The match lineup is determined by drawing lots. Someone asks the players about the lineup: a says he won't play against x, and c says he won't play against x or z. Please write code to find the match lineup for the three players. **Approach:** 1. Use **three loops** to enumerate all possible matchups between each player from Team A and players from Team B. 2. Add **constraints** to filter out combinations that don't meet the problem's requirements: * a does not play against x. * c does not play against x or z. 3. Ensure that each player from Team B is matched with only one player from Team A. ## Example ```c // Created by www. on 15/11/9. // Copyright Β© 2015 . All rights reserved. // #include int main() { // Define members of Team A and Team B char teamA[] = {'a', 'b', 'c'}; // Team A: a, b, c char teamB[] = {'x', 'y', 'z'}; // Team B: x, y, z // Variables i, j, k represent the opponents of a, b, c respectively char i, j, k; // Enumerate a's opponent for(i = 'x'; i<= 'z'; i++) { // Enumerate b's opponent for(j = 'x'; j<= 'z'; j++) { // Ensure a and b have different opponents if(i != j) { // Enumerate c's opponent for(k = 'x'; k<= 'z'; k++) { // Ensure c's opponent is different from a's and b's if(i != k && j != k) { // Meet problem conditions: a doesn't play x, c doesn't play x or z if(i != 'x' && k != 'x' && k != 'z') { // Output the match result printf("Match order: a--%ctb--%ctc--%cn", i, j, k); } } } } } } return 0; } The output of the above example is: Order: a--z b--x c--y [![Image 4: C Classic 100 Examples](#) C Classic 100 Examples](#)
← Ionic Creat AppIonic Install β†’