| Код | #include <iostream> #include <iomanip> #include <vector>
typedef std::vector<int> IntVector; typedef std::vector<IntVector> IntMatrix;
int calc_figure(const IntMatrix &matrix, int x, int y, int w, int h) { int result = 0; while(h--) { if (y >= matrix.size()) y -= matrix.size(); const IntMatrix::value_type &row = matrix[y]; for(int i = 0, c = x; i < w; ++i, ++c) { if (c >= row.size()) c -= row.size(); result += row[c]; } ++y; } return result; }
int main() { int rows, cols; // std::cout << "input columns count of matrix: " << std::flush; std::cout << "input matrix size: " << std::flush; std::cin >> cols; // std::cout << "input rows count of matrix: " << std::flush; // std::cin >> rows; rows = cols; IntMatrix matrix(rows, IntVector(cols)); std::cout << "You should separate all row element by space" << std::endl; for(int i = 0; i < rows; ++i) { std::cout << "Please enter matrix line #" << i << ": " << std::flush; for(int j = 0; j < cols; ++j) std::cin >> matrix[i][j]; }
int max_sum=matrix[0][0], max_x=0, max_y=0, max_w=1, max_h=1; for(int y = 0; y < rows; ++y) for(int x = 0; x < cols; ++x) for(int h = 1; h <= rows; ++h) for(int w = 1; w <= cols; ++w) { const int t = calc_figure(matrix, x, y, w, h); if (t > max_sum) { max_sum = t; max_x = x; max_y = y; max_w = w; max_h = h; } }
for(int i = 0; i < rows; ++i) { for(int j = 0; j < cols; ++j) std::cout << std::setw(3) << matrix[i][j]; std::cout << std::endl; } std::cout << "max sum found at (" << max_x << "," << max_y << ":" << max_w << "," << max_h <<") and equals to " << max_sum << std::endl; return 0; } |
|