【C】Judgment ranking of reasoning questions

1. Description of the topic

  5 athletes participated in the 10-meter platform diving competition, and they were asked to predict the result of the competition,

Player A said: B is second, I am third;

Player B said: I am second, E is fourth;

Player C said: I am first, D is second;

Player D said: C is last, I am third;

Player E said: I am fourth, A is first;

  After the game, each contestant is half right, please program to determine the ranking of the game.

2. Algorithm analysis

 First of all, there are a total of 5 players, then each person needs to traverse 5 times (there are 5 possibilities for each person's ranking), and then set the filtering conditions to get the ranking of each person. The key to solving such problems lies in the filtering conditions. . Since each player is half right, each player's statement can be converted to (either the former is true or the latter is true, either). Then the value of the expressions converted from the statements of the 5 players must be equal to 5 (that is, the value of each player's statements converted into expressions must be 1), but this filter condition is not enough to determine the ranking of the players. Because once a person has won the first place, it is impossible to get the second place. The ranking of each person must be a number between 1-5 and cannot be repeated. The value that meets this condition is the product of the rankings of five people. No matter how they are arranged, the product is always 1*2*3*4*5=120. To sum up, as long as the filter condition satisfies the expression value of 6, the ranking of each player can be obtained (ps: find a quantity that will not change).

3. Source code

#define _CRT_SECURE_NO_WARNINGS 1

/*
* Copyright (c) 2018, code farmer from sust
* All rights reserved.
*
* File name: JudgeRank.c
* Function: Solve the ranking
*
* Current version: V1.0
* Author: sustzc
* Completion date: April 4, 2018 23:07:24
*/

# include <stdio.h>

/*
* Function name: JudgeRank
*
* Function: judge ranking
*
* Entry parameter: void
*
* Exit parameter: void
*
* Return type: void
*/

void JudgeRank(void)
{
	int A = 0;
	int B = 0;
	int C = 0;
	int D = 0;
	int E = 0;

	for (A=1; A<=5; A++)
	{
		for (B=1; B<=5; B++)
		{
			for (C=1; C<=5; C++)
			{
				for (D=1; D<=5; D++)
				{
					for (E=1; E<=5; E++)
					{
						if (  6 == ( ((2 == B) && (3 != A)) || ((2 != B) && (3 == A)) )
							+ ( ((2 == B) && (4 != E)) || ((2 != B) && (4 == E)) )
							+ ( ((1 == C) && (2 != D)) || ((1 != C) && (2 == D)) )
							+ ( ((5 == C) && (3 != D)) || ((5 != C) && (3 == D)) )
							+ ( ((4 == E) && (1 != A)) || ((4 != E) && (1 == A)) )
							+ ( (120 == A*B*C*D*E) )  )
						{
							printf("A = %d, B = %d, C = %d, D = %d, E = %d\n",A, B, C, D, E);
						}
						else
						{
							;
						}
					}
				}
			}
		}
	}

	return;
}

int main(void)
{
	JudgeRank();

	return 0;
}

  Insufficiency: The if judgment condition is too cumbersome, and the condition of 5 == (A+B+C+D+E) is omitted. The slightly optimized code is given below.

/*
* Function name: JudgeRank
*
* Function: judge ranking
*
* Entry parameter: void
*
* Exit parameter: void
*
* Return type: void
*/

void JudgeRank(void)
{
	int A = 0;
	int B = 0;
	int C = 0;
	int D = 0;
	int E = 0;

	for (A=1; A<=5; A++)
	{
		for (B=1; B<=5; B++)
		{
			for (C=1; C<=5; C++)
			{
				for (D=1; D<=5; D++)
				{
					for (E=1; E<=5; E++)
					{
						if ( (1 == ((2 == B) + (3 == A))) && (1 == ((2 == B) + (4 == E)))
							&& (1 == ((1 == C) + (2 == D))) && (1 == ((5 == C) + (3 == D)))
							&& (1 == ((4 == E) + (1 == A))) && (120 == A * B * C * D *E)
							&& (15 == (A + B + C + D + E)) )
						{
							printf("A = %d, B = %d, C = %d, D = %d, E = %d\n",A, B, C, D, E);
						}
						else
						{
							;
						}
					}
				}
			}
		}
	}

	return;
}

4. Output results


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474889&siteId=291194637