【基础】某1个位上猜数字

一个最多九位的整数n,其中一个数位的数字未知(用?表示),问这个数位上使用哪些数字可以使其能被b整除。
如果无论填写什么数字都达不到要求则输出-1

#include<bits/stdc++.h>
using namespace std;
int b, pos, ans[10], cnt;
string s;
int to_int(int num){
    int re = 0;
    for(int i = 0; i < s.size(); i++){
        if(i == pos){
            re = re * 10 + num;
        }else{
            re = re * 10 + s[i] - '0';
        }
    }
    return re;
}
int main(){
    cin >> s >> b;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '?'){
            pos = i;
            break;
        }
    }
    for(int i = 0; i <= 9; i++){
        if(to_int(i) % b == 0){
            ans[++cnt] =i;
        }
    }
    if(!cnt) cout << -1 << endl;
    else{
        for(int i = 1; i <= cnt; i++){
            cout << ans[i] << " ";
        }
    }
    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^