概率題: 飛機座位 (升級挑戰版)

75 回覆
1 Like 23 Dislike
2024-10-05 21:42:26
我既 o1-preview答案同你唔同:

2024-10-05 21:47:12
有料
我o1-preview都係答1/2機率
2024-10-05 21:53:56
好明顯錯啦
2024-10-05 21:55:02
2024-10-05 21:58:13
2024-10-05 21:58:39
3個人的情況係7/18 佢地係咁話0.5
2024-10-05 22:03:13
我都係計到7/18 4個人冇計
本身諗住如果有人肯計3至5人嘅結果 我可能可以推到1000人嘅結果
點知3人嘅情況都冇乜人計到
2024-10-05 22:29:45
我諗緊第一條友坐左第10條友個位同坐左第100條友個位 導致第1000個可以坐到自己個位嘅機會好似唔同 咁就變左好似要窮舉所有case咁
2024-10-05 22:32:22
應該都幾複雜 所以唔想計
2024-10-05 22:37:10
收工
2024-10-05 22:46:26
用simulation估算應該最快
2024-10-05 22:50:32
3 passengers:
Prob=4/9~0.444%

1000 passengers:
Prob~0.577%

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int generate_random_number(int min, int max);
bool simulation(int n_passengers);

int main(void)
{
    int n_passengers;
    int n_runs;

    printf("Enter the number of passengers: ");
    scanf("%d", &n_passengers);
    printf("Enter the number of runs: ");
    scanf("%d", &n_runs);
    
    int success_count = 0;
    for (int i = 0; i < n_runs; i++)
    {
        if (simulation(n_passengers))
        {
            success_count++;
        }
    }
    printf("Success rate: %lf%%\n", (double) success_count / (double) n_runs);

    return 0;
}

int generate_random_number(int min, int max)
{
    return rand() % (max - min + 1) + min;
}

bool simulation(int n_passengers)
{
    int passengers_on_board_order[n_passengers];
    for (int i = 0; i < n_passengers; i++)
    {
        passengers_on_board_order[i] = i;
    }

    // Shuffle the array by Fisher-Yates algorithm
    for (int i = 0; i < n_passengers; i++)
    {
        int j = generate_random_number(0, n_passengers - 1);
        int temp = passengers_on_board_order[i];
        passengers_on_board_order[i] = passengers_on_board_order[j];
        passengers_on_board_order[j] = temp;
    }

    bool *restrict seats_taken = malloc(n_passengers * sizeof(bool));
    for (int i = 0; i < n_passengers; i++)
    {
        seats_taken[i] = false;
    }

    /* First passenger */
    int random_seat = generate_random_number(0, n_passengers - 1);
    if (random_seat == passengers_on_board_order[n_passengers - 1])
    {
        goto failure;
    }
    seats_taken[random_seat] = true;

    /* Second to the second last passenger */
    for (int i = 1; i < (n_passengers - 1); i++)
    {
        bool take_random_seat = true;
        if (seats_taken[passengers_on_board_order[i]] == false)
        {
            if (generate_random_number(0, 2) != 0)
            {
                seats_taken[passengers_on_board_order[i]] = true;
                take_random_seat = false;
            }
        }

        if (take_random_seat)
        {
            int random_seat = generate_random_number(0, n_passengers - 1);
            while (seats_taken[random_seat] == true)
            {
                random_seat = generate_random_number(0, n_passengers - 1);
            }
            if (random_seat == passengers_on_board_order[(n_passengers - 1)])
            {
                goto failure;
            }
        }
    }

    free(seats_taken);
    return true;

failure:
    free(seats_taken);
    return false;
}
2024-10-05 22:51:27
輕鬆
2024-10-05 22:52:38
唔洗 不過個算法我窮舉的話係999!每個可能性都叫電腦幫我計個機會率 會好慢
2024-10-05 22:55:31
Sor 有個bug
應該係
if (take_random_seat)
        {
            int random_seat = generate_random_number(0, n_passengers - 1);
            while (seats_taken[random_seat] == true)
            {
                random_seat = generate_random_number(0, n_passengers - 1);
            }
            if (random_seat == passengers_on_board_order[(n_passengers - 1)])
            {
                goto failure;
            }

            seats_taken[random_seat] = true;
        }

咁嘅話3 passengers仍然係4/9,但1000就係0.003%
2024-10-05 22:57:26
如果用窮舉去計算嗰個理論概率
O((n-1)!)

但如果你用實驗去估算大概嘅概率又唔知運行幾多次實驗比較好
2024-10-05 22:57:41
梗係用monte carlo,列哂所有可能性run到人類滅亡
2024-10-05 22:59:41
做convergence test,increase number of runs睇下個result會唔會好唔同

Btw 3個passenger我個result同上面ching個result唔同
2024-10-05 23:02:18
3人應該係7/18 不過都好接近

1000人答案接近0係合理嘅
2024-10-05 23:07:32
等陣先好似係 O((n-1)!*(n-1))


關於三個人嘅情況7除18就應該冇錯嘅
2024-10-05 23:09:10
你個隨機函數用 系統時間 做種子會唔會更加好
2024-10-05 23:13:41
我啱先手計都計到7/18,段code應該有啲問題
聽朝再搞
2024-10-06 00:51:33
Fix哂啲bug(應該)


Enter the number of passengers: 3
Enter the number of runs: 50000000
Success rate: 38.879478%


Enter the number of passengers: 1000
Enter the number of runs: 500000
Success rate: 0.302200%
2024-10-06 00:54:20
其實係咪放入python 就run到 定係點
定唔係python黎
2024-10-06 00:57:04
C嚟
不過translate去python應該好容易,揾gpt幫手
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞