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

โปรแกรมคำนวณความน่าจะเป็นของคีย์ K ในอาร์เรย์ C++

ภาพรวมปัญหา

โปรแกรมนี้คำนวณความน่าจะเป็นที่จะพบคีย์ (Key) K ในอาร์เรย์ โดยนิยามว่า:

P(K) = (จำนวนครั้งที่ K ปรากฏในอาร์เรย์) / (จำนวนองค์ประกอบทั้งหมดในอาร์เรย์)

ขั้นตอนการคำนวณ

  1. นับจำนวนองค์ประกอบทั้งหมดในอาร์เรย์ (n)
  2. นับจำนวนครั้งที่ K ปรากฏ (count)
  3. คำนวณความน่าจะเป็น = count / n
  4. ถ้า count = 0 ความน่าจะเป็น = 0

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

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

double probabilityOfKey(int arr[], int size, int key) {
    if (size == 0) return 0.0;
    
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] == key) count++;
    }
    
    return static_cast(count) / size;
}

int main() {
    // ตัวอย่างที่ 1
    int arr1[] = {1, 2, 3, 4, 5, 6};
    int key1 = 5;
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    
    cout << fixed << setprecision(6);
    cout << "Array: ";
    for (int x : arr1) cout << x << " ";
    cout << "\nKey: " << key1;
    cout << "\nProbability = " << probabilityOfKey(arr1, size1, key1) << endl;
    // count=1, size=6 → 1/6 = 0.166667
    
    cout << endl;
    
    // ตัวอย่างที่ 2: Key ไม่อยู่ในอาร์เรย์
    int arr2[] = {1, 2, 3, 4, 5, 6, 7};
    int key2 = 8;
    int size2 = sizeof(arr2) / sizeof(arr2[0]);
    
    cout << "Array: ";
    for (int x : arr2) cout << x << " ";
    cout << "\nKey: " << key2;
    cout << "\nProbability = " << probabilityOfKey(arr2, size2, key2) << endl;
    // count=0, size=7 → 0
    
    cout << endl;
    
    // ตัวอย่างที่ 3: Key ซ้ำหลายครั้ง
    int arr3[] = {2, 2, 3, 2, 4, 2, 5};
    int key3 = 2;
    int size3 = sizeof(arr3) / sizeof(arr3[0]);
    
    cout << "Array: ";
    for (int x : arr3) cout << x << " ";
    cout << "\nKey: " << key3;
    cout << "\nProbability = " << probabilityOfKey(arr3, size3, key3) << endl;
    // count=4, size=7 → 4/7 ≈ 0.571429
    
    return 0;
}

ผลลัพธ์ตัวอย่าง

Array: 1 2 3 4 5 6 
Key: 5
Probability = 0.166667

Array: 1 2 3 4 5 6 7 
Key: 8
Probability = 0.000000

Array: 2 2 3 2 4 2 5 
Key: 2
Probability = 0.571429

การขยาย: ความน่าจะเป็นของทุกค่าในอาร์เรย์

#include <map>
#include <vector>

map allProbabilities(int arr[], int size) {
    map freq;
    for (int i = 0; i < size; i++) {
        freq[arr[i]]++;
    }
    
    map prob;
    for (auto& p : freq) {
        prob[p.first] = static_cast(p.second) / size;
    }
    return prob;
}

// การใช้งาน
int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
int size = 10;
auto probs = allProbabilities(arr, size);
for (auto& p : probs) {
    cout << "P(" << p.first << ") = " << p.second << endl;
}

ผลลัพธ์การขยาย

P(1) = 0.1
P(2) = 0.2
P(3) = 0.3
P(4) = 0.4

ความซับซ้อนของอัลกอริทึม

  • เวลา: O(n) - ต้องสแกนอาร์เรย์หนึ่งครั้ง
  • พื้นที่: O(1) สำหรับคีย์เดียว, O(U) สำหรับทุกค่า (U = จำนวนค่าที่แตกต่างกัน)

การประยุกต์ใช้

  • การวิเคราะห์ความถี่ข้อมูล (Frequency Analysis)
  • การบีบอัดข้อมูล (Huffman Coding - ต้องการความน่าจะเป็น)
  • Machine Learning: คำนวณ Prior Probability
  • การทดสอบสมมติฐานทางสถิติ

ข้อควรระวัง

  • ใช้ double แทน float เพื่อความแม่นยำ
  • ตรวจสอบ size == 0 ก่อนหาร เพื่อป้องกัน Division by Zero
  • สำหรับอาร์เรย์ขนาดใหญ่ พิจารณาใช้ unordered_map แทน map เพื่อความเร็ว