简体   繁体   English

C++ - 任何人都可以帮助我将此for循环转换为递归function吗?

[英]C++ - Could any help me to translate this for loop to recursion function?

I have an array, table, with some char, a and b.我有一个数组,表,有一些字符,a 和 b。 I want to find and replace the 'b' with 'a' then count how many are replaced.我想找到并用'a'替换'b',然后计算替换了多少。 How to write a recursive function equivalent to the nested loop?如何编写等效于嵌套循环的递归 function?

    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;

This is what I tried:这是我尝试过的:

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;
}

My idea is, if it's not the end of the col or the row, then go check the next col or row.我的想法是,如果它不是列或行的末尾,那么 go 检查下一个列或行。 While checking, if the char is b, then return 1, and start checking from where it stops.检查时,如果 char 是 b,则返回 1,并从停止的位置开始检查。 But it is not working.但它不起作用。

The function can be defined the following way as it is shown in the demonstrative program function 可以如下定义,如演示程序所示

#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;
}

Its output is它的 output 是

5

Very close:很接近:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM