C/C++ basic questions | Find out the list of three pairs of players

According to the conditions and restrictions given in the title, we can write the following C language program to find the list of 3 pairs of players that meet the requirements:

#include <stdio.h>

int main() {
    char teamA[3] = {'A', 'B', 'C'};
    char teamB[3] = {'X', 'Y', 'Z'};
    char match[3][2];

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (teamA[i] == 'A' && teamB[j] == 'X')
                continue;
            else if (teamA[i] == 'C' && (teamB[j] == 'X' || teamB[j] == 'Z'))
                continue;
            else {
                match[i][0] = teamA[i];
                match[i][1] = teamB[j];
                break;
            }
        }
    }

    printf("比赛名单如下:\n");
    for (int i = 0; i < 3; i++) {
        printf("%c vs %c\n", match[i][0], match[i][1]);
    }

    return 0;
}

The program traverses the members of Team A and Team B through nested loops, and after excluding opponents that do not meet the requirements, stores the list of qualified players in a two-dimensional array, and finally outputs the list of matches match.

Running this program will get a list of three pairs that meet the requirements, for example:

比赛名单如下:
A vs Y
B vs Z
C vs Y

The above results meet the conditions in the title, A does not compete with X, and C does not compete with X and Z.


Attach the method that did not use GPT before


Permanent beginning: If you already have programming experience, please ignore this post, the basic questions will not help you much, just use the button directly.

These questions are based on Tan Haoqiang's "C Language Programming", the most commonly used book by college students. If you are dealing with final exams and upgrading to a higher education, it meets your needs, because this book is the standard for many exams.

Example 75: Two table tennis teams compete with 3 players each. Team A is A, B, C, 3 people, and Team B is X, Y, Z, 3 people. The list of matches has been determined by drawing lots. Someone asked the team members for the list of matches, A said he would not play against X, C said he would not play against X, Z, and the C language programming program found the list of 3 pairs of players.

Problem-solving idea: There is only one statement in the entire execution part, so there is only a semicolon at the end of the statement. Readers are asked to figure out the nesting relationship of loops and selection structures. The title is A, B, C, X, Y, Z, and the character constants 'X', 'Y', 'Z with apostrophes are used in the program. ',why is that? This is to directly output the characters A, B, C, X, Y, and Z at runtime to represent the situation of three groups of confrontation.

C language source code demo:

#include<stdio.h>//头文件
int main()//主函数
{
  char i,j,k;//定义字符变量
  for(i='x';i<='z';i++)//i是a的对手;j是b的对手;k是c的对手
  {
    for(j='x';j<='z';j++)
    {
      if(i!=j)
      {
        for(k='x';k<='z';k++)
        {
          if(i!=k&&j!=k)
          {
            if(i!='x'&&k!='x'&&k!='z')
            {
              printf("A--%c\nB--%c\nC--%c\n",i,j,k);//输出结果
            }
          }
        }
      }
    }
  }
  return 0;//主函数返回值为0
}

Compilation and running results are as follows:

A--z
B--x
C--y

--------------------------------
Process exited after 0.08659 seconds with return value 0
请按任意键继续. . .

The above code, the seniors used the most basic method to realize it, and readers can easily understand it, but it also caused several layers of nested loops. Interested readers can try to optimize it by themselves.

C++ source demo:

#include <iostream>
#include<cstdio>
using namespace std;

int main()//主函数
{
  char i,j,k;//定义字符变量
  for(i='x';i<='z';i++)//i是a的对手;j是b的对手;k是c的对手
  {
    for(j='x';j<='z';j++)
    {
      if(i!=j)
      {
        for(k='x';k<='z';k++)
        {
          if(i!=k&&j!=k)
          {
            if(i!='x'&&k!='x'&&k!='z')
            {
			  cout<<"A--"<<i<<endl;//输出结果
			  cout<<"B--"<<j<<endl;
			  cout<<"C--"<<k<<endl;
            }
          }
        }
      }
    }
  }
  return 0;//主函数返回值为0
}

Compile and run results:

A--z
B--x
C--y

--------------------------------
Process exited after 0.06169 seconds with return value 0
请按任意键继续. . .

 

Guess you like

Origin blog.csdn.net/qq_39154376/article/details/131929421