挺好玩的一道题目。这道题的意思是给出一个模糊值以及一个确定值,要求求出模糊值中大于确定值的个数有多少。
这题我是直接用dfs的方法搜索的,对于每一位如果之前位置的形成的数比确定值当前形成的数小,之后就不可能形成满足要求的值了。如果是大于的,之后的所有问号都可以填入0~9任何一个数字。如果是等于,就要继续搜索下一位。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; typedef long long LL;
char str1[], str2[]; LL dfs(int pos, LL cur1, LL cur2) {
LL ret = ;
if (cur1 < cur2) return ;
if (cur1 > cur2) {
ret = ;
while (str1[pos]) {
if (str1[pos] == '?') ret *= ;
pos++;
}
return ret;
}
if (str1[pos] == || str2[pos] == ) return ;
if (str1[pos] == '?') {
for (int i = ; i <= ; i++) {
if (i < str2[pos] - '') continue;
ret += dfs(pos + , cur1 * + i, cur2 * + str2[pos] - '');
}
} else {
ret += dfs(pos + , cur1 * + str1[pos] - '', cur2 * + str2[pos] - '');
}
return ret;
} int main() {
// freopen("in", "r", stdin);
while (cin >> str1 && str1[] != '#') {
cin >> str2;
cout << dfs(, , ) << endl;
}
return ;
}
——written by Lyon