C language | table tennis game, programming to find the list of 3 pairs of players

Example 59: Two table tennis teams compete, each with 3 people. Team A is composed of A, B, and C, with 3 people, and pair B is composed of X, Y, Z, with 3 people. Lots have been drawn to determine the roster. Someone asked the team members about the list of matches. A said he did not play with X, and C said he did not play with X or Z. The C language programming program found out 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. The reader is asked to figure out the nesting relationship between the loop and the selection structure. The title is given to A, B, C, X, Y, Z, and the character constants with apostrophes are used in the program.'X','Y','Z ',Why is that? This is to directly output the characters A, B, C, X, Y, and Z at runtime to indicate the situation of 3 groups of confrontation.

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 
}

The compilation and running results are as follows:

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

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

Kobayashi used the most basic method to implement the above code. Readers can easily understand it. However, it also causes several layers of loops to be nested. Interested readers can try to optimize it by themselves.

C language programming to find out the list of 3 pairs of competitors
More cases can go to the public account : C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112548501