C++中计算NxM矩阵每行都存在的数组元素个数


给定一个整数型元素数组和一个给定行和列大小的矩阵或二维数组,任务是计算存在于矩阵每一行中的数组元素的个数。

输入

int arr = { 2, 4, 6} and int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}}

输出

Elements of array in row 1 are: 3
Elements of array in row 2 are: 2
Elements of array in row 3 are: 2

解释

we are having array containing 2, 4 and 6 as elements and now we will check the occurrences of 3 elements in every row of matrix by matching the elements of an array with the elements of a matrix, like, 2, 4 and 6 all are present in first row of matrix so the count of elements for row 1 is 3, similarly, count of elements for row 2 is 2 as only 4 and 6 are there and count of elements for row 3 is 2 as only 2 and 6 are there.

输入

int arr = { 1, 3} and int matrix[row][col] = { { 1, 4, 6 }, {3, 1, 6}, {6, 2, 4}}

输出

Elements of array in row 1 are: 1
Elements of array in row 2 are: 2
Elements of array in row 3 are: 0

解释

we are having array containing 1 and 3 as elements and now we will check the occurrences of 2 elements in every row of matrix by matching the elements of an array with theelements of a matrix, like, only 1 is present in first row of matrix so the count of elements for row 1 is 1, similarly, count of elements for row 2 is 2 as 1 and 3 both are there and count of elements for row 3 is 0 as none of 1 and 3 are there.

下面程序中使用的方法如下

解决给定问题的方法有很多,例如:朴素方法和高效方法。 让我们先看看朴素方法

  • 输入一个整数元素数组和一个行和列大小的矩阵

  • 计算数组的大小,并将数组、矩阵和数组的大小传递给函数以进行进一步处理。

  • 使用一个名为count的临时变量来存储存在于矩阵行中的元素个数。

  • 从0开始循环到矩阵的行大小。

  • 在循环内部,从0开始循环到数组的大小。

  • 用arr[k]设置一个临时变量temp。

  • 开始另一个循环,从0循环到矩阵的列大小。

  • 在循环内部,检查IF temp = matrix[i][j],如果是,则将count加1。

  • 在每一行改变后,将count设置为0以刷新它。

  • 在每一行改变前打印count的值。

高效方法

  • 输入一个整数元素数组和一个行和列大小的矩阵

  • 计算数组的大小,并将数组、矩阵和数组的大小传递给函数以进行进一步处理。

  • 从0开始循环到矩阵的行大小。

  • 创建一个unordered_map类型的变量

  • 开始另一个循环,从0循环到矩阵的列大小。

  • 设置一个unordered_map,其中matrix[i][j]为1

  • 使用一个名为count的临时变量来存储存在于矩阵行中的元素个数。

  • 在循环内部,从0开始循环到数组的大小。

  • 检查IF um[arr[j]]==1,如果是,则将count加1。

  • 在每一行改变前打印count的值。

示例(朴素方法)

实时演示

#include<bits/stdc++.h>
using namespace std;
#define row 3
#define col 3
void arr_matrix(int matrix[row][col], int arr[], int size){
   int count = 0;
   //for matrix row
   for(int i=0; i<row; i++){
      //for array
      for(int k=0 ; k<size ; k++){
         int temp = arr[k];
         //for matrix col
         for(int j = 0; j<col; j++){
            if(temp == matrix[i][j]){
               count++;
            }
         }
      }
      cout<<"Elements of array in row "<< i + 1 <<" are: " << count << endl;
      count = 0;
   }
}
int main(){
   int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}};
   int arr[] = { 2, 4, 6};
   int size = sizeof(arr) / sizeof(arr[0]);
   arr_matrix(matrix, arr, size);
}

输出

如果运行以上代码,它将生成以下输出:

Elements of array in row 1 are: 3
Elements of array in row 2 are: 2
Elements of array in row 3 are: 2

示例(高效方法)

实时演示

#include <bits/stdc++.h>
using namespace std;
#define row 3
#define col 3
void arr_matrix(int matrix[row][col], int arr[], int size){
   for (int i = 0; i < row; i++){
      unordered_map<int, int> um;
      for (int j = 0; j < col; j++){
         um[matrix[i][j]] = 1;
      }
      int count = 0;
      for (int j = 0; j < size; j++) {
         if (um[arr[j]])
            count++;
      }
      cout<<"Elements of array in row "<< i + 1 <<" are: " << count << endl;
   }
}
int main(){
   int matrix[row][col] = { { 2, 4, 6 }, {3, 4, 6}, {6, 2, 1}};
   int arr[] = { 2, 4, 6};
   int size = sizeof(arr) / sizeof(arr[0]);
   arr_matrix(matrix, arr, size);
}

输出

如果运行以上代码,它将生成以下输出:

Elements of array in row 1 are: 3
Elements of array in row 2 are: 2
Elements of array in row 3 are: 2

更新于:2020年11月2日

672 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.