问号(深搜枚举)

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值