
Suppose we have one array. we have to count how many of the elements present in the array prime number of times. So if the array is {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, then 1 is present 4 times, 2 is present 3 times, 0 is present 3 times, and 5 is present 2 times. So there are three elements {2, 0, 5} that have occurred prime number of times. So the count will be 3.
所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。数组是在程序设计中,为了处理方便, 把具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。 数组应用&二维数组目录 1. 数组的简单应用2. 数组排序3. 数组查找4. 数组的使用思想5. 查表法6. 二维数组7. 数组综合
0
Begin
count := 0
define map with int type key and int type value
for each element e in arr, do
increase map.key(arr).value
done
for each key check whether the value corresponding the value is prime or not, if prime, then increase count.
return count
End#include <iostream>
#include <map>
using namespace std;
bool isPrime(int n){
for(int i = 2; i<=n/2; i++){
if(n % i == 0){
return false;
}
}
return true;
}
int countPrimeOcurrence(int arr[], int n){
int count = 0;
map<int, int> freq_map;
for(int i = 0; i<n; i++){
freq_map[arr[i]]++; //increase the frequency
}
for (auto it = freq_map.begin(); it != freq_map.end(); it++) {
if (isPrime(it->second))
count++;
}
return count;
}
int main() {
int arr[] = {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Prime frequency count: " << countPrimeOcurrence(arr, n);
}Prime frequency count: 3
以上就是数组元素的频率是否为质数?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号