题目
解决代码及点评
/*
在一个矩阵中,找到和最大的【2*2】的子矩阵
*/
#include <iostream>
using namespace std;
const int N = 4;
int main()
{
int index[2] = { 0 };
int max = 0;
// 待分析数据
int a[N][N] = { { 4, 5, 1, 6 }, { 1, 7, 10, 1 }, { 1, 5, 2, 2 }, { 12, 0, 3, 15 } };
// 遍历所有二维数据组
for (int i = 0; i < N - 1; i++)
{
for (int j = 0; j<N - 1; j++)
{
// 对二维数组求和,如果大于原来的max,则取代之
if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1]>max)
{
// 取代max,并保存坐标
max = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
index[0] = i;
index[1] = j;
}
}
}
// 输出信息
cout << "max " << max << endl;
cout << a[index[0]][index[1]] << " " << a[index[0] + 1][index[1]] << " " << a[index[0]][index[1] + 1] << " " << a[index[0] + 1][index[1] + 1];
system("pause");
return 0;
}
代码下载及其运行
代码下载地址:http://download.csdn.net/detail/yincheng01/6704519
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果