Description
给出两个字符串:s与t,长度一样。t只包含数字,而s除了数字还可能含有"?",问将s的问号变成数字后得到的数之中,有多少个数是大于t的。一个"?"只能变成一个数字。
Input
有多组测试数据。每组测试数据第一行是字符串s,第二行是字符串t。字符串的长度为length( 1 ≤ length ≤ 10 )。当遇到"#"时结束输入。
Output
为每组测试数据输出一行结果:大于t的数的个数。
Sample Input
36?1?8
236428
8?3
910
?
5
Sample Output
100
0
4
深搜枚举,注意要用long long。
#include<iostream>
#include<cstring>
#include<cstdlib>
typedef long long ll;
using namespace std;
ll sum,c,len,len2;
char a[20],b[20];
void dfs(ll i,ll m)///i为当前位置下标,m为当前得到的数
{
if(i==len){
if(m>c){
sum++;
}
return;
}
if(a[i]=='?'){
for(ll j=0;j<=9;j++){
dfs(i+1,m*10+j);
}
}
else{
dfs(i+1,m*10+(a[i]-'0'));
}
return;
}
int main()
{
while(cin>>a)
{
if(a[0]=='#')break;
sum=0;
cin>>b;
len=strlen(a);
len2=strlen(b);
if(len2>len)sum=0;
else{
c=atoll(b);///字符型转换成long long型
dfs(0,0);
}
cout<<sum<<endl;
}
return 0;
}