Practice the Blue Bridge Cup C language every day: violent enumeration

Hello, I am Fei Niao, welcome to read, if you have any questions, you can communicate with me, like it, and follow me (it is not easy to create, refuse to prostitute for nothing!)

When conducting inductive reasoning, if all possible situations of a certain type of event are examined one by one, and a general conclusion is drawn, then this conclusion is reliable. This induction method is called enumeration method.


  • Enumeration, also known as brute force, is a problem-solving strategy based on trying answers one by one.

  • When you first came into contact with algorithms, the basic algorithms you practiced were all about enumeration algorithms, such as printing out a 9 * 9 multiplication table, finding the number of daffodils, finding prime numbers, and so on. to find answers that satisfy the conditions.

  • The core idea of ​​the enumeration algorithm is to enumerate all possible

guess age

Problem Description:

American mathematician Wiener (N.Wiener) was precocious and went to university at the age of 11. He was invited to give lectures at Tsinghua University in China from 1935 to 1936. Once, he was attending an important meeting, and his young face was striking. So when someone asked his age, he replied: "My age cubed is a 4-digit number. My age is a 4-digit power. The 10 numbers contain exactly 10 numbers from 0 to 9, each It happened exactly once." Please calculate how young he was at that time. Submit his age number directly at that time .

Thought analysis:

Through the brute force method, find the approximate range of age, assuming his age is x, 10^3=1000, 30^4=8100, indicating that the range of x is between [10,30].

int main(){
    
    
	for(int i = 10;i <= 30;i++)
	{
    
    
		printf("%d= %.0f %.0f\n",i,pow(i,3),pow(i,4));//pow指i的立方,i的四次方
	}
	return 0;
}

result:

insert image description here

User age

Problem Description:

A new friend met a friend. When asked about his age, his netizen said: "My age is a 2-digit number, I am 27 years older than my son,

If I swap the two digits of my age, it happens to be my son's age."

Please calculate: How many possible situations are there for the age of netizens?

Hint: 30 years old is one of the possibilities.

Please fill in the number of possible situations.

Thought analysis:

The age of the netizen is 27 years older than his son, which means that the netizen is at least 27 years old, that is, the initial value is 27, the largest two-digit number is 99, and the range is [27,99]. The tens digit is 10 a, and the position of his age number is exactly the age of his son, then the age of his son is 10 b+a, the judgment condition is that the age of the netizen minus 27 is equal to the age of his son, and the sum is accumulated. Several possibilities.

#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int i,a,b,son,sum=0;

	for(i=27;i<100;i++){
    
    
		a=i/10;
	    b=i%10; //例如41对10整除取4,对10取余得1,即他儿子14岁
		son=(b*10)+a;
		
		if(27==i-son){
    
    
			printf("%d\n",i);
			sum++;
		}
		
	}
	printf("共有%d种可能性\n",sum);
	return 0;
}

result:

30
41
52
63
74
85
96
共有7种可能性

birthday age

Problem Description:

A certain gentleman has held a birthday party every year since a certain year, and every time he has to blow out the same number of candles as his age. Now counting, he blew out a total of 236 candles.

Excuse me, at what age did he start birthday parties?

Please output the age at which he started his birthday party.

Note: Your output should be an integer, do not output any superfluous or descriptive text.

enter

No input.

output

Output an integer, that is, the age at which a certain gentleman started his birthday party

hint

Use printf or cout to output the answer.

Thought analysis:

Suppose i is the initial age, j is the termination age, and j must be greater than i, then j=i+1, how many candles are k! Violence accumulates.

int main()
{
    
    

	for(int i=1;i<=100;i++)
	{
    
    
		for(int j=i+1;j<=100;j++)
		{
    
    
			int sum=0; 
			for(int k=i;k<=j;k++){
    
    
				sum += k;
			
			}
			if(sum==236)
			   printf("%d",i);
		}
	}
	return 0;
}

result:26

Math question

Problem Description:

limited five digits

How many five-digit numbers are there with a single digit of 6 that is divisible by 3?

Thought analysis:

First analyze the range of 5 digits [10000, 99999], the single digit is 6, which means at least two digits, and the remainder is 3, and ans is accumulated.

#include <stdio.h>
#include <stdlib.h>

int main() {
    
    
	int ans=0;
	for(int i=10000;i<=99999;i++){
    
    
		if((i*10+6)%3==0){
    
    
			ans++;
		}
	}
	printf("能被3整除的有%d个",ans);
	 
	return 0;
}

Result: There is a divisible by 30003

sloppy math

Problem Description:

Xiao Ming is an impatient person. When he was in elementary school, he often copied the questions written by the teacher on the blackboard by mistake. Once, the teacher asked the question: 36 x 495 =? He copied it: 396 x 45 = ? But the result was dramatic, his answer turned out to be right! ! Because 36 * 495 = 396 * 45 = 1782 There may be many other coincidences like this, for example: 27* 594 = 297 * 54

Suppose abcde represents 5 different numbers from 1 to 9 (note that they are different numbers and do not contain 0)

How many formulas can there be in the form: ab * cde = adb* ce? Please use the advantages of computer to find all the possibilities and answer the number of types of different calculations.

Expressions that satisfy the commutative law of multiplication count as different kinds, so the answer must be an even number.

Answers are submitted directly through the browser. Note: Only submit a number indicating the final number of statistical categories, do not submit the solution process or other superfluous content.

Thought analysis:

Assuming that abcde represents 5 different numbers from 1 to 9, the range of abcde is [1,9], how many results are accumulated by ans, and 1 is accumulated by judging that the condition ab cde=adb ce satisfies the condition, and then there is a possibility that a It is equal to b, so the judgment negates that a is not equal to b, and the same is true for other letters.

 #include<stdio.h>
 int main(){
    
    
 	int ans=0,m,n;
 	for(int a = 1;a<=9;a++){
    
    
 		for(int b = 1;b<=9;b++){
    
    
 			for(int c = 1;c<=9;c++){
    
    
 				for(int d =1;d<=9;d++){
    
    
 					for(int e =1;e<=9;e++){
    
    
 						
 						m = (a*10+b)*(c*100+d*10+e);
 						n = (a*100+d*10+b)*(c*10+e);
 						if(m==n&&a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e){
    
    
 							ans++;
 						}
 					}
 				}
 			}
 		}
 	}
 	printf("一共%d种",ans);
 	return 0;
 }

Results: 142

weird fraction

Problem Description:

When he was in elementary school, Xiao Ming often invented new algorithms by himself. Once, the teacher asked the question: 1/4 times 8/5 Xiaoming actually spliced ​​the numerators together and the denominators together, and the answer was: 18/45. The teacher just wanted to criticize him, but on second thought, this answer happened to be right, what the hell!

For the case where both the numerator and the denominator are one digit from 1 to 9, what other formulas can be calculated in this way? Please write down the number of all the different equations (including the examples in the question).

Obviously, after exchanging the numerator and denominator, for example: 4/1 times 5/8 is satisfactory, which counts as a different formula.

But with the same numerator and denominator, 2/2 times 3/3 are too many types to count!

Note: The answer is an integer (it must be an even number considering symmetry).

Please submit via browser. Don't write superfluous content.

Thought analysis:

In fact, it is similar to the previous one. Find the range, find out the judgment conditions, find out the non-conforming conditions, and use the cumulative ans

Note that you cannot use (a/b)*(c/d)==((a*10+c)/b*10+d)) , but use the cross multiplication in mathematics

 #include<stdio.h>
 int main(){
    
    
 	int ans=0;
 	for(int a=1;a<=9;a++){
    
    
 		for(int b =1;b<=9;b++){
    
    
 			for(int c =1;c<=9;c++){
    
    
 				for(int d=1;d<=9;d++){
    
    
 					if(a==b&&c==d)
 						continue;
 					int e = a*10+c,f = b*10+d;
 					if(a*c*f==b*d*e){
    
    
 						ans++;
 					}	
 					
 				}
 			}
 		}
 	}
 	printf("%d",ans);
 	return 0;
 }

Results: 14

geometry problem

Problem Description:

Xiaolan has an oversized warehouse that can hold a lot of goods. Now, Xiaolan has n boxes of goods to be placed in the warehouse, and each box of goods is a regular cube. Xiaolan stipulates three mutually perpendicular directions of length, width and height. The sides of each box of goods must be strictly parallel to the length, width and height. Little Blue hopes that all the goods will end up in a big cube. That is, stack L , W , and H goods in the directions of length, width, and height, respectively, satisfying n = L × W × H. Given n, how many options for stacking goods satisfy the requirement.

For example, when n = 4, there are the following 6 schemes: 1 × 1 × 4 , 1 × 2 × 2 , 1 × 4 × 1 , 2 × 1 × 2 , 2 × 2 × 1 , 4 × 1 × 1

Excuse me, when n = 2021041820210418 (note that there are 16 digits), how many options are there in total?
Tip: It is recommended to use computer programming to solve the problem.

Thought analysis:

The solution of cyclic violence enumeration, theoretically possible, but not feasible, convert the thinking and decompose all the factors (approximately) of the entire number, and then brute force all factors in two cycles to calculate the result.

 #include<stdio.h>
 int judge(long long a,long long b,long long c)
 {
    
    
 	if(a==b==c)//长宽高相同排列只有一种组合
	  return 1;
	 if(a==b&&a!=c||a==c&&a!=b||b==c&&a!=b) //长宽高 任意两个相同排列只有3种
	  return 3;
	 else
	   return 6; //长宽高都不相同排列只有6种
    
	   
 }
 int mian()
 {
    
    
 	long long n =2021041820210418;
 	long long L,W,H;
 	long long ans=0;
 	
 	for(L=1;L*L*L<=n;L++) //l是最小的一方
	 {
    
    
	 	if(n%L==0)//小优化
	  {
    
    
	 	 	
	 	 
	 	 for(W=L;L*W*W<=n;W++)//l是第二大的一方,次数多
	     {
    
    
	     	if(n%(L*W)==0)//小优化 
	     	{
    
    
	     		H=n/L/W;
				 if(H>=W){
    
    
				 	ans+=judge(L,W,H);
				 } 
	     	}
	     }
  }
	 	
	  
	 
    }
     printf("%d",ans);
    
	
	  
 } 

Result: 2430

insert image description here

Audience gentlemen, click on it!

Guess you like

Origin blog.csdn.net/A6_107/article/details/123435745