微软面试题解析:求一个矩阵中最大的二维矩阵(元素和最大)2014-12-25题目:求一个矩阵中最大的二维矩阵(元素和最大).如:
1 2 0 3 4
2 3 4 5 1
1 1 5 3 0
中最大的是:
4 5
5 3要求:(1)写出算法;(2)分析时间复杂度;(3)用C写出关键代码分析:直接遍历二维数组,求出最大的二维数组就OK了实现如下:
#include<iostream>using namespace std;int max_matrix(int (*array)[5], int maxx, int maxy, int& posi, int& posj){int max = 0;int i = 0, j = 0;while(i < maxx - 1){j = 0;while( j < maxy - 1){int t = array[i][j] + array[i+1][j] + array[i][j+1] + array[i+1][j+1];if( max < t){max = t;posi = i;posj = j;}j ++;}i ++;}return max;}int main(){int a[3][5] = {{1,2,0,3,4}, {2,3,4,5,1}, {1,1,5,3,0}};int i = 0, j = 0;int max = max_matrix(a, 3, 5, i, j);cout << "max num: " << max <<endl;cout << "matrix: " << endl;cout << a[i][j] << " " << a[i][j+1] << endl;cout << a[i+1][j] << " " << a[i+1][j+1] << endl;}
输出如下:max num: 17
matrix:
4 5
5 3作者:csdn博客 hhh3h