Mathematical thinking problems based on C language

Master Hung believes that the C language is broad and profound. A qualified programmer must not only have strict logical thinking, be proficient in MCU, operating system, and various industry-related things that sound very high, but also have mathematical thinking and a keen sense of smell. Excellent debugging ability. After a few years of mixing, I found that some very basic mathematical thinking has also become rigid; such as inequalities, such as various flexible applications of permutations and combinations, may be used less in normal times, but there is some free time, so you can practice more. Think more for yourself, so that your head won't become rigid.

Question 1: There are 1, 2, 3, and 4 numbers. How many different three-digit numbers can be formed without repeated numbers? How many are they?

Do not use the program first, just calculate how many kinds there are. If you remember the permutations and combinations in high school, the result is obviously A(3,4), which is 4 3 2 = 24 kinds.
Did any of the students here think of this?
So if you add another condition, how many different combinations are there in the 24 combinations? The difference here refers to the difference in disorder. For example, 123 and 321 are a combination.
There are 4 types calculated by permutation and combination formula, A(3,4)/A(3,3) = 4;

The above is a mathematical algorithm, how many kinds of calculations are there;
then it is very simple to arrange them with a C language program; the code is as follows:

#include"stdio.h"
void main()
{
    
    
	int arry[4] = {
    
    1,2,3,4};
	int i = 0,j = 0,k = 0;
	int number = 0;
	for(i = 0;i < 4;i++)
	{
    
    	
		for(j = 0;j < 4;j++)
		{
    
    	
			for(k = 0;k < 4;k++)
			{
    
    
				if((i != j)&&(i != k)&&(j != k)) 
				{
    
    
				printf(" %d ",arry[i]);
				printf(" %d ",arry[j]);
				printf(" %d\n",arry[k]);
				number++;
				}
			}	
		}
	}
	printf("number = %d\n",number);
} 在这里插入代码片

Question 2: An integer, after adding 100 to it, it becomes a perfect square number, and adding 168 is another perfect square number. What is the number?

Look at it at first glance. Everyone will inevitably form two formulas in their minds:
1. x+100 = n n;
2. x+100+168 = m
m;
what should we do next? Obviously there are three unknowns, but there are only two equations, it is certainly impossible to directly find the answer;
here we must use mathematical inequality ideas, there is no inequality to create inequalities;
then m m-n n = 168; the difference between m and n is 1. , The difference between the two is the smallest. If the difference between the squares of m and n is just greater than 168, then it is the upper limit of m and n, we can go to the exhaustion;
we can also use a similar method to know it The lower limit of m m + n n must be greater than 168, then the minimum n is 1, we can know that the minimum m is 13; this further narrows the scope; here
is the code: but this code does not Go for the lower limit. . .

int main()
{
    
    
	int m,n,i,j,x;
	for(m = 1,n = 0;m*m- n*n < 169;m++,n++);
	printf("m = %d\n",i = m);
	printf("n = %d\n",j = n);
	for(m=2;m<i+1;m++)
	{
    
    
		for(n=1;n<j+1;n++)
		{
    
    

			if((m*m - n*n) == 168)
			{
    
    
				printf("x = %d\n",n*n - 100);
			}
		}
	}
 } 

I have a problem-solving solution that is better than the above one. I will post the code:

int main()
{
    
    
	int i,j,m,n,x,q;
	for(i=2;i<85;i+=2)//由于i在分子部分,所以不能为0.不然直接死机 
	{
    
    
		if(168%i == 0)
		{
    
    
			j = 168/i;
			if(i > j && (i+j) %2 == 0 && (i-j)%2 == 0)
			{
    
    
				m = (i + j)/2;
				n = (i - j)/2;
				q = m*m - n*n;
				if(q == 168)
				{
    
    
					x = n*n - 100;
					printf("x = %d\n",x);
				}
			}
		}
	}
}

Continuation of the first solution:
1. x+100 = n n;
2. x+100+168 = m
m;
3. m m-n n = 168 -> (m+n) (mn) = 168, we set i = (m+n), j = (mn); then at least one of i and j is an even number;
4. Then proceed to the derivation, m = (i+j)/2, n = (ij)/2, so (I+j) or (ij) must be even numbers, so i and j are either even numbers or both odd numbers. According to the third step, we know that there is at least one even number, so both i and j are even numbers;
4. i
j = 168, if j = 2, then i must be the largest; so the maximum value of i is 84;
5. We will exhaust i from 2, knowing that i = 2, then we can certainly find j, m, n ,x, we know to find the value we want;

OVER 。。。。

Guess you like

Origin blog.csdn.net/qq_27854611/article/details/109587680