我的函数获取一组字符,大小,当前位置和计数器
我需要在输入尺寸的长度上打印A,B,C (无AA和无CCC)的所有可能组合。
当大小超过3时,函数的值返回一个非合理的值,我没有找到它的原因。
int abc(char arr[],int lastPlace,int curPlace,int count){
if (curPlace == lastPlace){
arr[curPlace] = '\0';
printf("%s, ", arr);
return 1;
}
if(!curPlace || arr[curPlace - 1] != 'A'){
arr[curPlace] = 'A';
count += abc(arr, lastPlace, curPlace + 1, 0);
}
arr[curPlace] = 'B';
count += abc(arr, lastPlace, curPlace + 1, 0);
if(curPlace < 2 || arr[curPlace - 1] != 'C' || arr[curPlace - 2] != 'C'){
arr[curPlace] = 'C';
count+=abc(arr, lastPlace, curPlace + 1, 0);
if (curPlace == 0)
return count;
}
}
答案 0 :(得分:-1)
int abc(char arr[], int lastPlace, int curPlace, int count)
{
if (curPlace == lastPlace)
{
arr[curPlace] = '\0';
printf("%s, ", arr);
return 1;
}
if(!curPlace || arr[curPlace-1]!='A'){
arr[curPlace] = 'A';
count += abc(arr, lastPlace, curPlace + 1, 0);
}
arr[curPlace] = 'B';
count += abc(arr, lastPlace, curPlace + 1, 0);
if(curPlace < 2 || arr[curPlace - 1] != 'C' || arr[curPlace - 2] != 'C')
{
arr[curPlace] = 'C';
count += abc(arr, lastPlace, curPlace + 1, 0);
}
return count;
}