首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一种递归函数,接受两个大小相同的矩阵,并输出它们是否相等

一种递归函数,接受两个大小相同的矩阵,并输出它们是否相等
EN

Stack Overflow用户
提问于 2018-11-16 23:32:30
回答 2查看 550关注 0票数 0

我正在尝试编写一个递归函数,它接受2个大小相等的矩阵,如果它们相等,则返回1,否则返回0。

代码语言:javascript
复制
int equal(int[][3], int[][3], int, int);
int main()
{
    int matrixOne[3][3] = { { 1, 2, 8 }, { 3, 4, 9 }, { 3, 3, 3 } };
    int matrixTwo[3][3] = { { 1, 2, 8 }, { 3, 4, 9 }, { 3, 3, 3} };
    cout << equal(matrixOne, matrixTwo, 2, 2);
    system("pause");
    return 0;
}
int equal(int matrixOne[][3], int matrixTwo[][3], int row, int column)
{
    if (row < 0)
        return 1;
    if (column < 0)
    {
        column = row;
        row--;
    }
    if (matrixOne[row][column] == matrixTwo[row][column])
        return equal(matrixOne, matrixTwo, row, column - 1);
    else
    {
        return 0;
    }
}

它不能正常工作,因为一旦列计数达到零,我们必须将它重置为原始的最大列计数,这可以在第一次通过column=row来完成;但之后它就不会遍历所有矩阵。

有没有可能使用这个函数原型来解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2018-11-17 00:06:41

你的函数将会工作,但我必须做一些修改:

  1. column = row替换为column = 2。请注意,在所提供代码的函数参数中,矩阵被声明为matrixOne[][3]。因为列是第二维的,所以我必须将2赋值给3。要使函数泛型,您必须更改代码。
  2. 在列检查之后放置对row < 0的检查。您可以在进行一些调试后找出原因;如果不这样做,递归将不会在预期的点处停止。

修改后的代码如下:

代码语言:javascript
复制
int equal(int[][3], int[][3], int, int);
int main()
{
    int matrixOne[3][3] = { { 1, 2, 8 }, { 3, 4, 9 }, { 3, 3, 3 } };
    int matrixTwo[3][3] = { { 1, 2, 8 }, { 3, 4, 9 }, { 3, 3, 3} };
    cout << equal(matrixOne, matrixTwo, 2, 2);
    system("pause");
    return 0;
}
int equal(int matrixOne[][3], int matrixTwo[][3], int row, int column)
{
    if (column < 0)
    {
        column = 2;
        row--;
    }
    if (row < 0)
        return 1;
    if (matrixOne[row][column] == matrixTwo[row][column])
        return equal(matrixOne, matrixTwo, row, column - 1);
    else
    {
        return 0;
    }
}

下面是我测试代码的链接:https://ideone.com/4tBpJg

票数 0
EN

Stack Overflow用户

发布于 2018-11-17 05:38:07

您可以很容易地创建一个通用版本,它允许矩阵具有任意数量的维度和任意大小的任何类型。您可以将数组的大小设置为模板参数,这样就不必传递它。

代码语言:javascript
复制
#include <type_traits>
#include <cstddef>

template<class T, std::size_t N>
typename std::enable_if<!std::is_array<T>::value, bool>::type
equal(T (&a)[N], T (&b)[N]) {
    for (std::size_t i = 0; i < N; ++i) {
        // T is not an array, (a is a single dimension array)
        // so just compare the values.
        if (a[i] != b[i]) {
            return false;
        }
    }
    return true;
}

template<class T, std::size_t N>
typename std::enable_if<std::is_array<T>::value, bool>::type
equal(T (&a)[N], T (&b)[N]) {
    for (std::size_t i = 0; i < N; ++i) {
        // T is an array (So a is a multidimensional array)
        // recursively call the "equal" function on the
        // lower dimensional arrays.
        if (!equal(a[i], b[i])) {
            return false;
        }
    }
    return true;
}

例如:

代码语言:javascript
复制
#include <iostream>

int main() {
    // Modified the size so you can see it better
    int matrixOne[2][3] = { { 1, 2, 8 }, { 3, 4, 9 } };
    int matrixTwo[2][3] = { { 1, 2, 8 }, { 3, 4, 9 } };

    /*
    equal(matrixOne, matrixTwo)
    // bool equal<int[3], 2> equal(int[3][2], int[3][2]);
        is equivalent to:
    equal(matrixOne[0], matrixTwo[0]) && equal(matrixOne[1], matrixTwo[1])
    // bool equal<int, 3> equal(int[3], int[3]);
        And they just compare the 3 values per column
    */

    // prints 1
    std::cout << equal(matrixOne, matrixTwo) << '\n';
}

但是,由于函数只是一个由“int”组成的多维数组,所以您可以只使用std::memcmp

代码语言:javascript
复制
#include <type_traits>
#include <cstddef>
#include <cstring>

// Change the above to
template<class T, std::size_t N>
typename std::enable_if<
    !std::is_array<T>::value && !std::is_trivially_copyable<T>::value, bool
>::type
equal(T (&a)[N], T (&b)[N]);

template<class T, std::size_t N>
typename std::enable_if<
    std::is_array<T>::value && !std::is_trivially_copyable<T>::value, bool
>::type
equal(T (&a)[N], T (&b)[N]);

// And the new one for trivial types
template<class T, std::size_t N>
typename std::enable_if<std::is_trivially_copyable<T>::value, bool>::type
equal(T (&a)[N], T (&b)[N]) noexcept {
    return std::memcmp(a, b, sizeof(a)) == 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53340921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档