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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP