Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 电话号码的字母组合_C++_Backtracking - Fatal编程技术网

C++ 电话号码的字母组合

C++ 电话号码的字母组合,c++,backtracking,C++,Backtracking,给定一个数字字符串,我们需要打印数字代表的所有字母组合 对于输入“23”,输出应为[“ad”、“ae”、“af”、“bd”、“be”、“bf”、“cd”、“ce”、“cf”] 类解决方案{ 公众: [10[10[4][4][4][4][4][4][[4]={{{[0 0,,,[0,,,,,[0,,,0,,,,,,0,,,,[0,,,,[10[10[10[10[10[10,,[0,,,,[0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

给定一个数字字符串,我们需要打印数字代表的所有字母组合

对于输入“23”,输出应为[“ad”、“ae”、“af”、“bd”、“be”、“bf”、“cd”、“ce”、“cf”]

类解决方案{
公众:
[10[10[4][4][4][4][4][4][[4]={{{[0 0,,,[0,,,,,[0,,,0,,,,,,0,,,,[0,,,,[10[10[10[10[10[10,,[0,,,,[0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}};
向量ans;
无效打印(字符串数字、字符串st、整数位置)
{
int i,l=数字.size();
如果(l==pos)
{
ans.推回(st);
返回;
}
其他的
{

对于(i=pos;i而言,问题在于您同时在循环和递归

print("22", "", 0);
将再次出现在

print("22", "a", 1);
print("22", "b", 1);
print("22", "c", 1);
print("22", "a", 2);
print("22", "b", 2);
print("22", "c", 2);
你的多余部分是最后三次通话

消除输入数字上的循环(您已经通过递归完成了该步骤):

void打印(字符串数字、字符串st、整数位置)
{
if(digits.size()==pos)
{
ans.推回(st);
}
其他的
{
int ch=数字[pos]-“0”;
对于(int j=0;j<4&&ph[ch][j]!='0';j++)
{
打印(数字,st+ph[ch][j],位置+1);
}
}
}

(您还忘记终止某些数组,但这是另一个问题。)

解决此类问题的正确工具是调试器。在询问堆栈溢出问题之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]你的问题将包括一个重现你的问题的示例,以及你在调试器中所做的观察。另外,为了将来:你的标题似乎与问题没有任何关系。这里没有提到电话号码,更不用说电话号码中没有字母。代码根本没有解释,因此没有人知道是什么这是一种非常糟糕的提问方式。当函数不打印任何内容时,为什么称为“打印”呢?
print("22", "a", 1);
print("22", "b", 1);
print("22", "c", 1);
print("22", "a", 2);
print("22", "b", 2);
print("22", "c", 2);
 void print(string digits, string st, int pos)
 {
     if(digits.size() == pos)
     {
         ans.push_back(st);
     }
     else
     {
         int ch = digits[pos] - '0';
         for(int j = 0; j < 4 && ph[ch][j] != '0'; j++)
         {
             print(digits, st + ph[ch][j], pos + 1);
         }
     }
 }