HDU 3699 A hard Aoshu Problem

本文介绍了一种解决Aoshu数学难题的编程算法,通过暴力枚举的方法找出所有可能的解决方案。详细解释了输入格式、输出要求以及解决思路,包括如何将字母替换为数字并验证等式是否成立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A hard Aoshu Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 345    Accepted Submission(s): 173


Problem 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.
 

Sample Input
  
2 A A A BCD BCD B
 

Sample Output
  
5 72
 

Source

模拟题,每个字母代表一个数字,最多有五个字母,暴力枚举即可。

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

int tot, sct, lena, lenb, lenc;
char s1[10], s2[10], s3[10], save[10], save0[10], dig[10];
char ss1[10], ss2[10], ss3[10], soso[10];
void solve(int cct)
{
    if(cct >= sct)
    {
        int aa, bb, cc;
        for(int i = 0; i < sct; i++)
        {
            for(int j = 0; j < lena; j++)
                if(ss1[j] == save[i])
                    s1[j] = dig[i];
            for(int j = 0; j < lenb; j++)
                if(ss2[j] == save[i])
                    s2[j] = dig[i];
            for(int j = 0; j < lenc; j++)
                if(ss3[j] == save[i])
                    s3[j] = dig[i];
            sscanf(s1, "%d", &aa);
            sscanf(s2, "%d", &bb);
            sscanf(s3, "%d", &cc);
        }
        if(aa * bb == cc)
            ++tot;
        if(aa - bb == cc)
            ++tot;
        if(aa + bb == cc)
            ++tot;
        if(bb != 0 && aa / bb == cc && aa % bb == 0) // 必须是整除
            ++tot;
        return ;
    }
    int flag;
    if(strlen(save0) && strchr(save0, save[cct]) != NULL)
        flag = 1;
    else
        flag = 0;
    for(int j = flag; j < 10; j++)
        if(strchr(soso, ('0' + j)) == NULL)
        {
            soso[cct] = '0' + j;
            dig[cct] = '0' + j;
            solve(cct + 1);
            soso[cct] = '\0';
        }
    return ;
}
int main()
{
#ifdef test
    freopen("in.txt", "r", stdin);
#endif
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int zct = 0;
        tot = sct = 0;
        memset(s1, 0, sizeof(s1));
        memset(s2, 0, sizeof(s2));
        memset(s3, 0, sizeof(s3));
        memset(save, 0, sizeof(save));
        memset(save0, 0, sizeof(save0));
        memset(soso, 0, sizeof(soso));
        scanf("%s%s%s", ss1, ss2, ss3);
        strcpy(s1, ss1);
        strcpy(s2, ss2);
        strcpy(s3, ss3);
        lena = strlen(s1), lenb = strlen(s2), lenc = strlen(s3);
        if(strchr(save0, s1[0]) == NULL && lena > 1)
            save0[zct++] = s1[0];
        if(strchr(save0, s2[0]) == NULL && lenb > 1)
            save0[zct++] = s2[0];
        if(strchr(save0, s3[0]) == NULL && lenc > 1)
            save0[zct++] = s3[0];
        for(int i = 0; i < lena; i++)
            if(strchr(save, s1[i]) == NULL)
                save[sct++] = s1[i];
        for(int i = 0; i < lenb; i++)
            if(strchr(save, s2[i]) == NULL)
                save[sct++] = s2[i];
        for(int i = 0; i < lenc; i++)
            if(strchr(save, s3[i]) == NULL)
                save[sct++] = s3[i];
        solve(0);
        printf("%d\n", tot);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值