Make up the formula (full array dfs)

make up the formula

    B      DEF
A +---- + ------- = 10
    C      GHI

(If there is a problem with the display, please refer to [Figure 1.jpg])

In this formula, A I represents the numbers 1 to 9, and different letters represent different numbers.

For example:
6+8/3+952/714 is one solution,
5+3/1+972/486 is another solution.

How many solutions are there to this equation?
29
Note: Your submission should be an integer, do not fill in any superfluous content or descriptive text.
insert image description here

Problem- solving idea 1: Use the next_permutation(a,a+n) function
Note that the fractional addition and subtraction array should be defined as a double type, and the following sum should also be a double type including 10;
the use of the next_permutation() function is explained below

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int a[4]={1,2,3,4};
    do{
        cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
    }while(next_permutation(a,a+4));
    system("pause");
    return 0;
}

That is, find the next permutation of the content in parentheses. For example: a[4]={1,2,3,4}.next_permutation(a,a+4) is 1,2,4,3.
Function prototype:
If it is 4,3,2,1.next_permutation The return value is false because there is no next permutation. But after one function call, it will become 1, 2, 3, 4.
In contrast, there is another function called prev_permutation() As the name suggests, this function is to find the previous permutation.
The explanation in Baidu Encyclopedia is as follows:
The next_permutation function will generate the next larger permutation of the given sequence in alphabetical order until the whole sequence is in descending order. The prev_permutation function does the opposite, generating the previous smaller permutation of the given sequence. The principle of the two is the same, but the order of the examples is reversed
. Question: How to find the next permutation of an array?
Suppose the array is 3 6 4 2. The next permutation should be 4 2 3 6.
Procedure: For an arbitrary sequence, the smallest permutation is in increasing order and the largest in descending order.
Looking forward from the last digit, the first thing you get is 2, and a simple number does not need to be exchanged.
Then we get 4 2, 4 is greater than 2, it is already the largest sequence in this subsequence, and a larger sequence cannot be excluded.
Then you get 6 4 2, the same principle as above.
After that, 3 6 4 2 is obtained. At this time, since 3 is less than 6 and less than 4, the next permutation of 3 6 4 2 should be the smallest one of the permutations larger than the permutation of 3 6 4 2, so 3 should be the same as 4. Swap, and the arrangement becomes 4 6 3 2. At this time, 4 is at the beginning, so the sequence after that should be the smallest sequence, that is, 2 3 6. To sum up, the final result should be 4 2 3 6.

#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
double a[10] = {1,2,3,4,5,6,7,8,9};
int ans = 0;
int main(){
	while(next_permutation(a,a + 9)){
		double sum = a[0] + (a[1] / a[2]) + (a[3] *100 + a[4] * 10 + a[5]) / (a[6] * 100 + a[7] * 10 + a[8]);
		if(sum == 10.0){
			ans++;
		} 	
	}
	cout << ans;
	return 0;
}

Problem- solving idea 2: dfs recursive search
Note that the fraction addition and subtraction array should be defined as a double type, and the following sum should also be a double type including 10;
or * 1.0 after the numerator means a number converted to a double type

#include <iostream>
using namespace std;
double a[9] = {0};
int ans = 0;
bool judge(double a[]){
	double sum = a[0] + (a[1] * 1.0 / a[2]) + (a[3] *100 + a[4] * 10 + a[5]) * 1.0/ (a[6] * 100 + a[7] * 10 + a[8]);
	if(sum == 10.0){
		return true;
	} 
	return false;
}
bool check(int num){
	for(int i = num - 1;i >= 0;i--){
		if(a[i] == a[num]){//注意判断重复的条件
			return false;
		}
	}
	return true;
}
void dfs(int x){
	if(x > 8){
		if(judge(a)){
			ans++;
		}
		return;
	}
	for(int  i = 1;i < 10;i++){
		a[x] = i;
		if(check(x)){//注意检查的是参数的值 不是循环i的值
			dfs(x + 1);	
		}
	}
}
int main(){
	dfs(0);
	cout << ans;
	return 0;
} 

Problem-solving idea 3: Violence

Because division is involved in fractions, try not to use division, because it involves precision, I tried once and got the wrong answer (the int and double types were not considered at that time), you can
use the following method to compare after dividing
It can be converted to double type in the following way

if(a + ((b * 1.0) / c) + (d * 100 + e * 10 + f) * 1.0 /(g * 100 + h * 10 + j) == 10){
				ans += 1;
 } 
#include<iostream>
#include <algorithm>
using namespace std;
int main(){
	int ans = 0;
	int a,b,c,d,e,f,g,h,j;
	for(int a = 1;a <= 9;a++){
		for(int b = 1;b <= 9;b++){
			if(a == b){
				continue;
			}
			for(int c = 1;c <= 9;c++){
				if(a == c || b == c){
					continue;
				}
				for(int d = 1;d <= 9;d++){
					if(a == d || b == d || c == d){
						continue;
					}
					for(int e = 1;e <= 9;e++){
						if(a == e || b == e || c == e || d == e){
							continue;
						}
						for(int f = 1;f <= 9;f++){
							if(a == f || b == f || c == f || d == f || e == f){
								continue;
							}
							for(int g = 1;g <= 9;g++){
								if(a == g || b == g || c == g || d == g || e == g || f == g){
									continue;
								}
								for(int h = 1;h <= 9;h++){
									if(a == h || b == h || c == h || d == h || e == h || f == h || g == h){
										continue;
									}
									for(int j = 1;j <= 9;j++){
										if(a == j || b == j || c == j || d == j || e == j || f == j || g == j || h == j ){
											continue;
										}
										/*if(a + ((b * 1.0) / c) + (d * 100 + e * 10 + f) * 1.0 /(g * 100 + h * 10 + j) == 10){
												ans += 1;
					 					} */
										int t1 = a * c * ( 100 * g + 10 * h + j ); 
                                        int t2 = b * (100 * g + 10 * h + j); 
                                        int t3 = c * (100 * d + 10 * e + f); 
                                        int t4 = 10 * c * (100 * g + 10 * h + j); 
                                        if( t1 + t2 + t3 == t4)ans++;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	cout << ans;
	return 0;
}

Guess you like

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