HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)

Description

Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem:
ABBDE __ ABCCC = BDBDE
In the equation above, a letter stands for a digit(0 � 9), and different letters stands for different digits. You can fill the blank with ‘+’, ‘-‘ , ‘×’ or ‘÷’.  
How to make the equation right? Here is a solution:
12245 + 12000 = 24245
In that solution, A = 1, B = 2, C = 0, D = 4, E = 5, and ‘+’ is filled in the blank.
When I was a kid, finding a solution is OK. But now, my daughter’s teacher tells her to find all solutions. That’s terrible. I doubt whether her teacher really knows how many solutions are there. So please write a program for me to solve this kind of problems.
 

Input

The first line of the input is an integer T( T <= 20) indicating the number of test cases.
Each test case is a line which is in the format below:
s1 s2 s3  
s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8.
When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation.  
You should figure out the number of solutions making the equation right.
Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero.
 

Output

For each test case, print an integer in a line. It represents the number of solutions.

 

题目大意:给一个最多5个字母的式子,要求用不同的数字替换这些字母,中间填一个符号,问有多少种填法能使等式成立。

思路:暴力枚举。

PS:易错点:不能有前导0,。可以有单个0。一个数字只能出现一次。除法不能用除号(整除的问题)。做除法前要判断被零除的问题(移项了不代表不用判断)。有些字母可能不在式子里出现。

 

代码(15MS):

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 using namespace std;

 6 

 7 const int MAXN = 12;

 8 

 9 char s1[MAXN], s2[MAXN], s3[MAXN];

10 int trans[MAXN];

11 bool exist[MAXN], use[MAXN];

12 int ans;

13 

14 int s_to_i(char *s) {

15     if(trans[s[0] - 'A'] == 0 && s[1]) return -1;

16     int ret = 0;

17     for(int i = 0; s[i]; ++i)

18         ret = ret * 10 + trans[s[i] - 'A'];

19     return ret;

20 }

21 

22 void dfs(int dep) {

23     if(dep == 5) {

24         int a1 = s_to_i(s1), a2 = s_to_i(s2), a3 = s_to_i(s3);

25         if(a1 == -1 || a2 == -1 || a3 == -1) return ;

26         if(a1 + a2 == a3) ++ans;

27         if(a1 - a2 == a3) ++ans;

28         if(a1 * a2 == a3) ++ans;

29         if(a2 && a1 == a2 * a3) ++ans;

30         return ;

31     }

32     if(!exist[dep]) {

33         dfs(dep + 1);

34         return ;

35     }

36     for(int i = 0; i <= 9; ++i) {

37         if(use[i]) continue;

38         trans[dep] = i;

39         use[i] = true;

40         dfs(dep + 1);

41         use[i] = false;

42     }

43 }

44 

45 void check(char *s) {

46     for(int i = 0; s[i]; ++i)

47         exist[s[i] - 'A'] = true;

48 }

49 

50 int main() {

51     int T;

52     scanf("%d", &T);

53     while(T--) {

54         scanf("%s%s%s", s1, s2, s3);

55         memset(exist, 0, sizeof(exist));

56         check(s1), check(s2), check(s3);

57         ans = 0;

58         dfs(0);

59         printf("%d\n", ans);

60     }

61 }
View Code

 

你可能感兴趣的:(test)