设为首页 加入收藏

TOP

求二维数组子数组和中最大的值(一)
2014-07-19 23:04:39 来源: 作者: 【 】 浏览:261
Tags:二维数 最大

  题目

  求二维数组中子数组和中最大的值,及子数组

  方案

  我们已经做过一维数组的问题。如果你没有看过一维数组的问题,请戳   求子数组之和最大值

  二维数组是一维数组的数组,这样理解起来,我们就不费事了

  我的算法是先将压缩列,即每一列的和作为新数组的元素来组成新的数组来简化问题,然后就变成一维数组求最大子数组问题了

  具体算法设计用C++表示如下

  // 求出二维数组的最大子二维数组

  int Array::Max_Sum_Sub_Double_Array(int **data,const unsigned int row,const unsigned int column)

  {

  // 异常输入

  if(data == NULL || row == 0 || column == 0)

  {

  cout《"异常输入 Max_Max_Sub_Double_Array"《endl;

  return -1;

  }

  // 正常输入

  else

  {

  // 核心算法数据初始化

  // 按照一位数组来处理列,把每一列作为一个数

  int * OneArray = new int[column];

  for( unsigned int i=1;i <= column;i++ )

  {

  OneArray[i-1] = Sum_Sub_Column_Double_Array(data,row,column,i,1,row);

  }

  unsigned int L,R;

  Border_Max_Sum_Sub_Array(OneArray,column,L,R);

  delete [] OneArray;

  // 按照一位数组来处理行,把每一行作为一个数

  OneArray = new int[row];

  for(unsigned int i=1;i <= row;i++)

  {

  OneArray[i-1] = Sum_Sub_Row_Double_Array(data,row,column,i,L,R);

  }

  unsigned int U,D;

  Border_Max_Sum_Sub_Array(OneArray,row,U,D);

  cout《U《" "《D《" "《L《" "《R《endl;

  return Max_Sum_Sub_Array(OneArray,row);

  }

  }

  // 求出二维数组中的某一列的子数组的和

  int Array::Sum_Sub_Column_Double_Array(int ** data,const unsigned int row,const unsigned int column,unsigned int mycolumn,unsigned int s,unsigned int e)

  {

  // 异常输入

  if(data == NULL || row == 0 || column == 0 || mycolumn > column || s>e || e>row)

  {

  cout《"异常输入 Sum_Sub_Column_Double_Array"《endl;

  return -1;

  }

  // 正常输入

  else

  {

  int sum = 0;

  for(unsigned int i = s;i <= e;i++)

  {

  sum += data[i-1][mycolumn-1];

  }

  return sum;

  }

  }

  // 求出二维数组中的某一行的子数组的和

  int Array::Sum_Sub_Row_Double_Array(int **data,const unsigned int row,const unsigned int column,unsigned int myrow,unsigned int s,unsigned int e)

  {

  // 异常输入

  if(data == NULL || row == 0 || column == 0 || myrow > row || s>e || e> column)

  {

  cout《"异常输入 Sum_Sub_Row_Double_Array"《endl;

  }

  // 正常输入

  else{

  int sum = 0;

  for(unsigned int i=s;i <= e;i++)

  {

  sum += data[myrow-1][i-1];

  }

  return sum;

  }

  }

   

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++堆和栈空间的区别 下一篇C++的那些事:面向对象

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·CPython是什么?PyPy (2025-12-26 06:50:09)
·Python|如何安装seab (2025-12-26 06:50:06)
·python要学习数据分 (2025-12-26 06:50:03)
·每日一道面试题-多线 (2025-12-26 06:20:17)
·java项目中哪些地方 (2025-12-26 06:20:14)