【问题标题】:Recursive Divide & Conquer Technique递归分治法
【发布时间】:2018-02-25 06:59:06
【问题描述】:
int **matMult(int **mat1,int **mat2,int **res,int N){

    static int i=0,j=0,k=0;
    if(i>=N)
    return 0;

    else if(i<N) 
    {
      if(j<N) 
      {
         if(k<N)
         {
             res[i][j]+=mat1[i][k]*mat2[k][j];
             k++;
             matMult(mat1,mat2,res,N);
         }

             k=0;
             j++;
             matMult(mat1,mat2,res,N);
      }
        j=0;
        i++;
        matMult(mat1,mat2,res,N);
    }
return res;
}

输出: 输入 N 的值:64 分段错误(核心转储)

64 大小后显示此错误

【问题讨论】:

  • 你试过 N = 2 吗?可能更容易找到问题。
  • 如果你要乘以 8192 x 8192 大小的矩阵,不要使用递归。它很可能会导致堆栈溢出。改用更简单、更容易理解的循环方法。
  • @RSahu 很可能会导致堆栈溢出。 - 是的。堆栈溢出,或此页面上的问题,或两者兼而有之。 :)

标签: c


【解决方案1】:

代码本身没有明显的问题。如果它适用于较小的N,则可以怀疑N = 64的堆栈溢出。

使用递归进行矩阵乘法是众所周知的方法:

Matrix Multiplication

Matrix multiplication using the Divide and Conquer paradigm

对于实际的 C 解决方案检查:

Matrix Multiplication using divide and conquer approach

Strassen-recursive-matrix-multiplication.c

顺便说一句:您的代码与此处发布的代码非常相似:C Program to Perform Matrix Multiplication using Recursion

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 2011-12-06
    • 2014-04-21
    • 1970-01-01
    • 2020-04-11
    • 2021-05-11
    • 2017-08-19
    • 2015-07-10
    • 2017-07-16
    相关资源
    最近更新 更多