Blue Bridge Cup 2013 4th C language group B provincial exercise problem solution-exercise B. sloppy formula

Daily brush questions (102)

Sloppy formula

Title description

Xiao Ming is impatient. When he was in elementary school, he often copied the questions his teacher had written 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, and his answer turned out to be correct! !
Because 36 * 495 = 396 * 45 = 17820
there may be many coincidences like this, such as: 27 * 594 = 297 * 54
assuming that abcde represents 5 numbers from 1 to 9 (note that they are different numbers, and Excluding 0)
How many kinds of calculations can satisfy the form: ab * cde = adb * ce?

Subject requirements

Please use the advantages of computers to find all the possibilities and answer the types and numbers of different calculations.
The formulas that satisfy the commutative law of multiplication are counted as different types, so the answer must be an even number.

Submit request

The answer is submitted directly through the browser.
Note: Only submit a number that represents the final statistical category, do not submit the answering process or other redundant content.

Ideas

In fact, this is a simple question of dfs, first arrange all, then judge, and then ans++

C++ code

#include<bits/stdc++.h>
using namespace std;

int ans = 0;
int n = 5;
int a[6] = {
    
    0};
int book[6] = {
    
    0};

void dfs(int step)
{
    
    
	if(step == n + 1)
	{
    
    
		if((a[1] * 10 + a[2]) * (a[3] * 100 + a[4] * 10 + a[5]) == ((a[1] * 100 + a[4] * 10 + a[2]) * (a[3] * 10 + a[5])))
			ans++;
		return;
	}
	
	for(int i = 1; i < 10; i++)
	{
    
    
		if(book[i] == 0)
		{
    
    
			a[step] = i;
			book[i] = 1;
			dfs(step + 1);
			book[i] = 0;
		} 
	}
}

int main()
{
    
    
	dfs(1);
	cout << ans << endl;
	return 0;
} 

Operation result:
Insert picture description here
So the answer is: 142

If you like my article, please remember three consecutive times, like and follow the collection, every like and every one of your attention and every collection will be my infinite motivation on the way forward! ! ! ↖(▔▽▔)↗Thank you for your support, the next issue will be more exciting! ! !

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/114995386