维度数组[矩阵]练习的问题

时间:2019-01-19 21:02:35

标签: c++ matrix

我们有一个方矩阵A = [aij],其中行和行的数目大于3。目标是:在矩阵A中找到表D [3; 3]的位置,其中元素的总和为最高的。通过指定最左侧元素的索引来指定此位置。

此刻,我做了什么:已经写了一个代码,创建了一个维数组,所有这些:C


int main() {
  int n;
  size_t height, weight;
  cout << "Input height and weight of your matrix:" << endl;
  cin >> dlina >> weight;
  int **a = new int*[height];
  for (int i = 0; i < height; i++)
    a[i] = new int[weight];

  for (int i = 0; i < height; i++)
    for (int j = 0; j < weight; j++) {
      cout << "Enter your matrix element: " << endl;
      cin >> a[i][j];
    }

  for (int i = 0; i < height; i++){ //i=0

    for (int j = 0; j < weight; j++) {//j=7
      cout << a[i][j] << " ";
    }

    cout << endl;

    }


  for (int i = 0; i < height; i++)
    delete[] a[i];
  delete[] a;


  cin >> n; 

  return 0;
}

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案对您的代码进行了最小的更改 ,包括我之前的评论:

#include <iostream>
using namespace std;

const int N = 3;

int sum(int * a[], size_t i, size_t j)
{
  int n = 0;

  for (size_t ii = i; ii != i + N; ++ii)
    for (size_t jj = j; jj != j + N; ++jj)
      n += a[ii][jj];

  return n;
}

int main() {
  size_t height, width;
  cout << "Input height and width of your matrix:" << endl;
  cin >> height >> width;

  if ((height < N) || (width < N))
    return 0;

  int **a = new int*[height];
  for (size_t i = 0; i < height; i++)
    a[i] = new int[width];

  for (size_t i = 0; i < height; i++) {
    for (size_t j = 0; j < width; j++) {
      cerr << "Enter your matrix element: " << i << ' ' << j << ":";
      cin >> a[i][j];
    }
  }

  int max = sum(a, 0, 0);
  size_t maxi = 0, maxj = 0;

  for (size_t i = 1; i <= (height - N); i++){
    for (size_t j = 0; j <= (width - N); j++) {
      int s = sum(a, i, j);

      if (s > max) {
        max = s;
        maxi = i;
        maxj = j;
      }
    }
  }

  cout << maxi << ' ' << maxj << " : " << max << endl;

  for (size_t i = 0; i < height; i++)
    delete[] a[i];

  delete[] a;

  return 0;
}

执行示例:

Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288

注意:可以优化此琐碎的解决方案,以免每次NxN矩阵移动时都重做所有单元格的总和,让我做...


valgrind 下执行:

==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767== 
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767== 
==13767== HEAP SUMMARY:
==13767==     in use at exit: 0 bytes in 0 blocks
==13767==   total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767== 
==13767== All heap blocks were freed -- no leaks are possible
==13767== 
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)