i到j行的数组相加，得到一个一维数组。在整个一维数组上求和最大的连续子数组。

`#include <iostream>using namespace std;int *sum_i_j(int **data,int cols,int i,int j){    int *sum=new int[cols];    memset(sum,0,sizeof(int)*cols);    for(int col=0;col<cols;col++){        for(int row=i;row<=j;row++){            sum[col]+=data[row][col];        }    }    return sum;}int maxSum(int *dataCols,int cols){    int max=dataCols[0];    int *sum=new int[cols];    memset(sum,0,sizeof(int)*cols);    sum[0]=dataCols[0];    for(int i=1;i<cols;i++){        sum[i]=dataCols[i];        if(sum[i]<(sum[i-1]+dataCols[i])){            sum[i]=sum[i-1]+dataCols[i];        }        if(sum[i]>max)            max=sum[i];    }    delete sum;    return max;}int maxSubSum(int **data,int rows,int cols){    int max=-0x3f3f3f3f;    int *sumTmp=new int[cols];    for(int i=0;i<rows;i++){        for(int j=i;j<rows;j++){            sumTmp=sum_i_j(data,cols,i,j);            int tmp=maxSum(sumTmp,cols);            if(tmp>max){                max=tmp;            }        }    }    delete sumTmp;     return max;} int main(){    /************************************************************************/    /*     4 4    0 -2 -7 0    9 2 -6 2    -4 1 -4 1    -1 8 0 -2                                                                     */    /************************************************************************/    int rows,cols;    cin>>rows>>cols;    int **data=new int*[rows];//二维数组传值很麻烦    for(int i=0;i<rows;i++){        data[i]=new int[cols];///        for(int j=0;j<cols;j++){            cin>>data[i][j];;       }    }    cout<<maxSubSum(data,rows,cols)<<endl;    system("pause");    return 0;}`

`#include <iostream>#include <assert.h>using namespace std;/*最大子数组之和*/  int MaxSubSum(int nArr[],int nLen)  {      assert(nArr && nLen > 0);      int nMaxSum = nArr[0];      int nCurSum = nArr[0];      for (int i = 1;i < nLen;i++)      {          if (nCurSum < 0)          {              nCurSum = nArr[i];          }          else          {              nCurSum += nArr[i];          }          nMaxSum = max(nCurSum,nMaxSum);      }      return nMaxSum;  }  /*把原矩阵第i行和第j行之间元素进行压缩，形成一个一维数组*/void InitSumArr(int** pnArr,int** pnArrColSum,int nXLen,int nYLen){    assert(pnArr && *pnArr && pnArrColSum && *pnArrColSum);    assert(nXLen > 0 && nYLen > 0);    for (int i = 0;i < nXLen;i++)//横坐标    {        for (int j = 0;j < nYLen;j++)//纵坐标        {            pnArrColSum[i][j] = 0;            for (int t = 0;t <= i;t++)            {                pnArrColSum[i][j] += pnArr[t][j];            }        }    }}/*枚举二维数组，压缩成一维数组，求解最大子数组和*/int MaxSubMatrixSum(int** pnArr,int** pnArrColSum,int nXLen,int nYLen){    assert(pnArr && *pnArr && pnArrColSum && *pnArrColSum);    assert(nXLen > 0 && nYLen > 0);    int nMaxSum = -0x3f3f3f3f;    int nCurSum = -0x3f3f3f3f;    int* pTmpArr = new int[nYLen];     for (int i = 0;i < nXLen;i++)    {        for (int j = i;j < nXLen;j++)        {            if (i == 0)            {                for (int t = 0;t < nYLen;t++)                {                    pTmpArr[t] = pnArrColSum[j][t];                }                nCurSum = MaxSubSum(pTmpArr,nYLen);                nMaxSum = max(nCurSum,nMaxSum);            }            else            {                //计算每列元素和,并求最大子数组之和                for (int t = 0;t < nYLen;t++)                {                    pTmpArr[t] = pnArrColSum[j][t] - pnArrColSum[i - 1][t];                }                nCurSum = MaxSubSum(pTmpArr,nYLen);                nMaxSum = max(nCurSum,nMaxSum);            }        }    }    return nMaxSum;}int main(){    int nXLen = 0;    int nYLen = 0;    cin>>nXLen>>nYLen;    int** pnArr = new int*[nXLen];    int** pnArrColSum = new int*[nXLen];    for (int i = 0;i < nXLen;i++)    {        pnArr[i] = new int[nYLen];        pnArrColSum[i] = new int[nYLen];        for (int j = 0;j < nYLen;j++)        {            cin>>pnArr[i][j];        }    }    InitSumArr(pnArr,pnArrColSum,nXLen,nYLen);    cout<<MaxSubMatrixSum(pnArr,pnArrColSum,nXLen,nYLen)<<endl;    system("pause");    return 1;}`