acdream 1704(暴力)

来源:互联网 发布:喀秋莎微课制作软件 编辑:程序博客网 时间:2024/05/21 06:41

题意:
Problem Description

小晴天是ACdream团队中最牛的老师之一,他最擅长数学运算~这天他翻开一本《AC is not a dream》杂志,发现最后一页有一道很经典的思维题,题目很简单,每个框填写一个数字,构成一个竖式,每个数的最高位不能为0,但是有一些数字被隐藏掉了,然后让你根据没有隐藏的数字填出隐藏的数字。

如下图:

然后小晴天二话不说,三下五除二就写出了答案:

然后小晴天就觉得这样的题目太简单了,于是问你是否有办法来求出一道题目有多少种不同的答案呢?(只要有一个方框有不同的数字即为不同的答案)
Input

多组数据,首先是一个整数t(t<=20),表示数据组数。
对于每组数据,用5行表示一个竖式,每行均为一个字符串,仅含有星号(*)与数字(‘0’~’9’)组成,其中星号表示空白
其中第一行为长度为3的字符串。
第二行为长度为2的字符串。
第三行为长度为4的字符串。
第四行为长度为3的字符串。
第五行为长度为5的字符串。
Output

对于每组数据,输出一个整数x,表示符合乘法竖式法则的填法的种类。
Sample Input

2


**
3384
846


4**
**
3384
846


Sample Output

2
1
题解:因为第一个乘数3位数,枚举从100到999,第二个乘数2位数,枚举从10到99,先去匹配已有值,不满足continue,否则得到第三个数字和第四个数字,再去匹配,不满足continue,否则得到最后一个数字,匹配成功答案加一。

#include <stdio.h>#include <string.h>char str[10][10];int s[10][10];int res = 0, flag[5] = {3, 2, 4, 3, 5};void solve(int x) {    for (int i = 0; i < flag[x]; i++)        if (str[x][i] != '*')            s[x][i] = str[x][i] - '0';        else            s[x][i] = -1;}bool judge(int x, int cur) {    int a = flag[cur] - 1, temp[10];    while (x > 0 && a >= 0) {        int b = x % 10;        if (s[cur][a] != -1 && s[cur][a] != b)            return false;        a--;        x /= 10;    }    if (x == 0 && a == -1)        return true;    return false;}int main() {    int t;    scanf("%d", &t);    while (t--) {        for (int i = 0; i < 5; i++) {            scanf("%s", str[i]);            solve(i);        }        res = 0;        for (int i = 100; i < 1000; i++) {            if (!judge(i, 0))                continue;            for (int j = 10; j < 100; j++) {                if (!judge(j, 1))                    continue;                int temp1 = i * (j % 10);                if (!judge(temp1, 2))                    continue;                int temp2 = i * (j / 10);                if (!judge(temp2, 3))                    continue;                int temp = temp1 + temp2 * 10;                  if (!judge(temp, 4))                    continue;                res++;            }        }        printf("%d\n", res);    }    return 0;}
0 0
原创粉丝点击