c++ - 打印从数字创建的可能字符串

标签 c++ c algorithm combinatorics

给定一个 10 位数字的电话号码,我们必须打印由此创建的所有可能的字符串。数字的映射与手机键盘上的数字映射完全相同。

即对于 1,0-> 无字母 对于 2-> A,B,C

例如,1230 平均日增重 BDG CDG AEG....

C/C++ 中解决此问题的最佳解决方案是什么?

最佳答案

我认为递归解决方案适合这个问题。所以类似:

def PossibleWords(numberInput, cumulative, results):
    if len(numberInput) == 0:
        results.append(cumulative)
    else:
        num = numberInput[0]
        rest = numberInput[1:]
        possibilities = mapping[num]
        if len(possibilities) == 0:
            PossibleWords(rest, cumulative, results)
        else:
            for p in possibilities:
                PossibleWords(rest, cumulative + p, results)

result = []
PossibleWords('1243543', '', result)

关于c++ - 打印从数字创建的可能字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1727124/

相关文章:

python - 使用 boost.python,如何扩展类的 __dir__ 函数?

c++ - QLabel 不使用 QToolButton 显示图像

C - getchar() 没有正确读取输入的第二个字符

c - 跟踪并保存代码流程

c++ - 如何计算下面的struct占用的内存空间?

c++ - 使用 boost iostreams 过滤器(关闭且不可复制)

c - 在 malloc 实体内存块中使用标准数组索引

algorithm - 扩展节点是什么意思?

algorithm - 使用距离变换算法的形状突发

javascript - 这个gpu操作的算法?