Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

นับสมาชิกในอาร์เรย์ที่มีความถี่เป็นจำนวนเฉพาะ ในภาษา C

นับจำนวนค่าที่แตกต่างกันในอาร์เรย์ที่ปรากฏบ่อยเป็นจำนวนเฉพาะ

ตัวอย่าง

arr = {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}

ความถี่: 1→4, 2→3, 0→3, 5→2

จำนวนเฉพาะ: 3, 2 → ค่าที่มีความถี่เป็นเฉพาะ: {2, 0, 5} → 3 ค่า

โค้ดตัวอย่าง (C++)

#include <iostream>
#include <map>
using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i*i <= n; i++) if (n%i==0) return false;
    return true;
}

int countPrimeFreq(int arr[], int n) {
    map<int,int> freq;
    for (int i=0;i

ผลลัพธ์

Prime frequency count: 3