我们得到了一个整数类型的元素数组以及一个给定行和列大小的矩阵或二维数组,任务是计算矩阵每一行中存在的数组元素的数量。
输入项
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.
解决给定问题的方法可以有多种,即幼稚方法和有效方法。因此,让我们首先来看一下幼稚的方法。
输入整数元素数组和行和列大小的矩阵
计算数组的大小,并将数组,矩阵和数组的大小传递给函数以进行进一步处理
进行临时变量计数以存储矩阵行中存在的元素计数。
从0开始循环FOR直到矩阵的行大小
在循环内,从0开始直到数组的大小为FOR
用arr [k]设置温度
从0到矩阵的列大小开始另一个循环FOR
在循环内,检查IF temp = matrix [i] [j],然后将计数增加1
将count设置为0以在每行更改后刷新它
在每行更改之前打印计数值。
输入整数元素数组和行和列大小的矩阵
计算数组的大小,并将数组,矩阵和数组的大小传递给函数以进行进一步处理
从0开始循环FOR直到矩阵的行大小
创建类型为unordered_map的变量
从0到矩阵的列大小开始另一个循环FOR
将矩阵[i] [j]设置为1的无序映射
进行临时变量计数以存储矩阵行中存在的元素计数。
在循环内,从0开始直到数组的大小为FOR
检查IF um [arr [j]] == 1,然后将计数增加1
在每行更改之前打印计数值。
#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(int i=0; i<row; i++){ //对于数组 for(int k=0 ; k<size ; k++){ int temp = arr[k]; //对于矩阵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