C++ - 能帮我把这个 for 循环翻译成递归函数吗?
C++ - Could any help me to translate this for loop to recursion function?
我有一个数组 table,其中包含一些字符 a 和 b。
我想找到 'b' 并将其替换为 'a',然后计算替换了多少。如何写一个等价于嵌套循环的递归函数?
const int length = 4;
char table[length][length] = {
{'a','b','a','a'},
{'a','a','a','b'},
{'a','a','b','a'},
{'b','b','a','a'}
};
int count = 0;
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (table[i][j] == 'b') {
count++;
table[i][j] = 'a';
}
}
}
cout << "Count: " << count << endl;
这是我试过的:
int replace_char(char array[][length], int row, int col) {
// base cases and recursive
if (row+1 != length - 1)
replace_char(array, row+1, col);
if (col+1 != length - 1)
replace_char(array, row, col+1);
// do this
if (array[row][col] == 'b') {
array[row][col] = 'a';
return 1 + replace_char(array, row, col);
}
return 0;
}
我的想法是,如果不是列或行的末尾,则去检查下一个列或行。检查时,如果char为b,则return1,从停止处开始检查。
但它不起作用。
函数可以按照演示程序中所示的方式定义
#include <iostream>
size_t replace_count( char *s, size_t n, char c1 = 'b', char c2 = 'a' )
{
return n == 0 ? 0
: ( ( *s == c1 ? ( *s = c2, 1 ) : 0 ) + replace_count( s + 1, n - 1, c1, c2 ) );
}
int main()
{
const int length = 4;
char table[length][length] = {
{'a','b','a','a'},
{'a','a','a','b'},
{'a','a','b','a'},
{'b','b','a','a'}
};
std::cout << replace_count( reinterpret_cast<char *>( table ), length * length )
<< '\n';
return 0;
}
它的输出是
5
非常接近:
int replace_char(char array[][length], int row, int col) {
if (col == length) {
return replace_char(array, row + 1, 0); // reach the end of a col increment the
// row by 1 and reset the col.
// We are over the end so return
}
if (row == length) {
// Then we have passed the last row simply return.
return 0;
}
// Now do the work.
int count = 0;
if (array[row][col] == 'b') {
array[row][col] = 'a';
count = 1;
}
// Once you have done the work
// Make the recursive call.
return count + replace_char(array, row, col + 1);
}
我有一个数组 table,其中包含一些字符 a 和 b。 我想找到 'b' 并将其替换为 'a',然后计算替换了多少。如何写一个等价于嵌套循环的递归函数?
const int length = 4;
char table[length][length] = {
{'a','b','a','a'},
{'a','a','a','b'},
{'a','a','b','a'},
{'b','b','a','a'}
};
int count = 0;
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (table[i][j] == 'b') {
count++;
table[i][j] = 'a';
}
}
}
cout << "Count: " << count << endl;
这是我试过的:
int replace_char(char array[][length], int row, int col) {
// base cases and recursive
if (row+1 != length - 1)
replace_char(array, row+1, col);
if (col+1 != length - 1)
replace_char(array, row, col+1);
// do this
if (array[row][col] == 'b') {
array[row][col] = 'a';
return 1 + replace_char(array, row, col);
}
return 0;
}
我的想法是,如果不是列或行的末尾,则去检查下一个列或行。检查时,如果char为b,则return1,从停止处开始检查。 但它不起作用。
函数可以按照演示程序中所示的方式定义
#include <iostream>
size_t replace_count( char *s, size_t n, char c1 = 'b', char c2 = 'a' )
{
return n == 0 ? 0
: ( ( *s == c1 ? ( *s = c2, 1 ) : 0 ) + replace_count( s + 1, n - 1, c1, c2 ) );
}
int main()
{
const int length = 4;
char table[length][length] = {
{'a','b','a','a'},
{'a','a','a','b'},
{'a','a','b','a'},
{'b','b','a','a'}
};
std::cout << replace_count( reinterpret_cast<char *>( table ), length * length )
<< '\n';
return 0;
}
它的输出是
5
非常接近:
int replace_char(char array[][length], int row, int col) {
if (col == length) {
return replace_char(array, row + 1, 0); // reach the end of a col increment the
// row by 1 and reset the col.
// We are over the end so return
}
if (row == length) {
// Then we have passed the last row simply return.
return 0;
}
// Now do the work.
int count = 0;
if (array[row][col] == 'b') {
array[row][col] = 'a';
count = 1;
}
// Once you have done the work
// Make the recursive call.
return count + replace_char(array, row, col + 1);
}