p [1]大于100000试验

I am getting p1 >50k for 100000 trials which is incorrect, and that's why after printing 1 line of output the code doesn't print anything else.This is the question statement -

生日问题。 假设人们一次进入一个房间。人们必须如何进入,直到两个人过生日?违反直觉的是,当23个人进入房间后,两个人大约有50-50的机会分享生日。这种现象被称为生日问题或生日悖论。 编写一个程序Birthday.java,它接受两个整数命令行参数n并进行试验,并执行以下试验(试验次数):

Choose a birthday for the next person, uniformly at random between 0 and n−1. Have that person enter the room. If that person shares a birthday with someone else in the room, stop; otherwise repeat. In each experiment, count the number of people that enter the room. Print a table that summarizes the results (the count i, the number of times that exactly i people enter the room, and the fraction of times that i or fewer people enter the room) for each possible value of i from 1 until the fraction reaches (or exceeds) 50%. this is the link for the question

    public class Birthday {
public static void main(String[] args) {
    int n = Integer.parseInt(args[0]);
    int triaLs = Integer.parseInt(args[1]);
    boolean[] birthdAy = new boolean[n];
    int[] p = new int[n + 2]; // no of times i people enter the room
    int[] aVg = new int[n]; // to sum subsequent p[i]
    double[] average = new double[n];

    for (int i = 0; i < triaLs; i++) {
        for (int j = 1; j <= n; j++) {
            int c = (int) (Math.random() * (n - 1));
            if (!birthdAy[c]) {
                birthdAy[c] = true;
                continue;
            }
            else if (birthdAy[c])
                p[j]++;
            break;
        }
        // adding the subsequent p[]
        for (int k = 1; k < n; k++) {
            aVg[k] = aVg[k - 1] + p[k];
            average[k] = (double) aVg[k] / triaLs;
        }
    }
    for (int i = 0; i < n; i++)
        if (average[i] <= 0.5)
            System.out.println(i + "  " + p[i] + "  " + average[i]);
}
评论