电话号码的问题,书中递归方法有问题,主要是当执行过程中,如果发现有数字没有对应号码,那此时就不会进行递归,则此时是错误的,所以在进入递归之前,应该先进行判断,代码如下:
void recursivePrint(int *number, int *answer, int index, int n)
{
if( index == n ){
for (int i = 0; i < n; i++) {
printf("%c", c[number[i]][answer[i]]);
}
printf("\n");
return;
}
if(total[number[index]] == 0 && index != n-1){
answer[index]=0;
recursivePrint(number, answer, index+1, n);
}
else {
for(answer[index]=0; answer[index]<total[number[index]]; answer[index]++){
recursivePrint(number, answer, index+1, n);
}
}
}