Simple right and wrong judgment and Realization of Yang Hui triangle

catalogue

1. Simple right and wrong judgment

2. Yanghui triangle

1. Simple right and wrong judgment

Five athletes participated in the 10m platform diving competition. They were asked to predict the result of the competition:
Player A said: B second, I third;
Player B said: I'm second, E is fourth;
Contestant C said: I am the first and D is the second;
Player D said: C finally, I'm third;
Player E said: I'm fourth, A is first;
After the competition, each contestant is half right. Please program to determine the ranking of the competition.
 

Analysis: at this stage, what I can think of is a simple and crude exhaustive method, which exhausts all possible cycles, and then tests whether the conditions are right one by one

Steps:

1. Several people set several variables, and then analyze which method to describe them (for example, right or wrong is 01, ranking is 12345...)

2. Set a loop inside the loop and give out each result

3. The consistency of judgment and condition in the cycle. Of course, pay attention to the judgment of logic in the process, and discrete mathematics can be learned

4. In fact, after exhaustive listing, the rest may have problems. For example, two people are in the fifth place at the same time, while the third place does not have this kind of situation, so we have to limit the results

//
//int main()
//{
//    int a = 0, b = 0, c = 0, d = 0, 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 (((b == 2) + (a == 3) == 1) &&
//                            ((b == 2) + (e == 4) == 1) &&
//                            ((c == 1) + (d == 2) == 1) &&
//                            ((c == 5) + (d == 3) == 1) &&
//                            ((e == 4) + (a == 1) == 1))
//                        {
//                            if (a * b * c * d * e == 120)
//                            {
//                                printf("a=%d b=%d c=%d d=%d e=%d", a, b, c, d, e);
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//    return 0;
//}

 

2. Yanghui triangle

High school knowledge shows that the number of column j in row I is C (j-1, i-1), and it's bad to write when you know the law

Restrictions:

1. I don't know how to practice C language, so I have to make it up by myself. It's nothing more than C (n, m) = n * (n-1) ** (n-m+1)/m! , this can be done in two cycles

2. The first line can't do according to the loop we want to write, so just judge it in the first line if (), and if it is 1

Steps:

1. Create a num for how many lines you want to print and cycle several times

2. Set the cyclic printing of lines, the first line will print 1, and the others will use the law of C (j-1, i-1)

3. However, there is another problem. In the first column of row i (i > 1), if C (n, m) = n * (n-1) ** (n-m+1)/m! It directly becomes zero, because the output is 0. Anyway, the first column must be 1. When j==1, print 1; Similarly, j==i cannot be realized, so it is directly judged as 1

4. Write a similar C (j-1, i-1), that is, we want to find C (n, m) = n * (n-1) ** (n-m+1)/m!  . First of all, we must have numerator and denominator. Let's set the variables son and mom (which are intuitively named). Mom is the factorial of j-1, so we can do it once. Mom is from I-1 to I + 1-J, so the loop can also be done, and then divide and print

5. Don't forget to type a new line after each line.

int main()
{
	int num = 0;
	scanf("%d", &num);
	for (int i = 1; i <= num; i++)
	{
		if (i == 1)
		{
			printf("1");
		}
		else
		{
			for (int j = 1; j <= i; j++)
			{
				if (j == 1)
				{
					printf("1 ");
				}
				if (j == i)
				{
					printf("1 ");
				}
				if (1 < j && j < i)
				{
					int son = 1;
					int mom = 1;
					for (int k = 1; k <= j - 1; k++)
					{
						mom *= k;
					}
					for (int k = 1; k <= j - 1; k++)
					{
						son *= i - k;
					}
					printf("%d ",son/mom);
				}
			}
		}
		printf("\n");
	}
	return 0;
}

Tags: C

Posted by ruuyx on Wed, 25 May 2022 01:28:23 +0300