0

代码是运行模拟来找出 n 个人生日相同的概率。

我将随机分配的生日与一组日期进行了比较。对于任何具有超过 1 个相等值的日期,我在分子上加了一个。

但是,代码的答案是错误的。我不确定为什么。

import java.util.Scanner;
public class birthday {
public static void main (String[] args) {

Scanner inp = new Scanner(System.in);

System.out.println("How many trials");
int n = inp.nextInt();

//variable declaration
double[] birthdate = new double[n];
int num = 0;
int numerator = 0;
double bday = 0;
int trials = 0;

//assign birthdays to n people
for (int i = 0; i < n; i++)
{
    birthdate[i] = Math.floor(Math.random() * 365) + 1;
    System.out.println(birthdate[i]);
}

for (int i = 1; i <= 365; i++)
{
    for (int j = 0; j < n; j++)
    {
        bday = birthdate[j];

        //compare birthdates to dates
        if (bday == i)
            {
                num++;
                if (num > 1)
                {
                    numerator++;
                }
            }
    }
    num = 0;
}

double ans = (double) numerator / n;
System.out.println("The answer is " + ans);

}
}
4

1 回答 1

1

对于任何具有超过 1 个相等值的日期,我在分子上加了一个。

这不是你的代码所做的。对于在该日期至少有 2 人过生日的任何日期,您将这些人的数量减去 1 加到分子中。

如果您希望您的代码按照上面的语句工作,您必须更改以下代码

for (int j = 0; j < n; j++)
{
    bday = birthdate[j];

    //compare birthdates to dates
    if (bday == i)
        {
            num++;
            if (num > 1)
            {
                numerator++;
            }
        }
}
num = 0;

到这段代码:

for (int j = 0; j < n; j++)
{
    bday = birthdate[j];

    //compare birthdates to dates
    if (bday == i)
        {
            num++;
        }
}
if (num > 1)
{
    numerator++;
}
num = 0;

这样,代码if (num > 1) numerator++不会为每个人重复(从第二个开始),而是每个日期只重复一次。

无论如何,我怀疑任何一个版本的代码都会计算出“n 个人共享同一生日的概率”。如果这是您想要近似的结果,您应该多次重复整个实验,计算在这些案例中有多少人共享他们的生日,然后将其除以实验次数:

import java.util.Scanner;

public class birthday {
    public static void main(String[] args) {

        Scanner inp = new Scanner(System.in);

        System.out.println("How many trials?");
        int numExperiments = inp.nextInt();

        System.out.println("How many persons?");
        int n = inp.nextInt();

        // variable declaration
        int dups = 0;
        for (int k = 0; k < numExperiments; k++) {
            boolean foundDup = false;
            int[] birthdate = new int[n];

            // assign birthdays to n people
            for (int i = 0; i < n; i++) {
                birthdate[i] = (int) (Math.random() * 365) + 1;
            }

            // check, if there is a duplicate
            for (int i = 1; i <= 365; i++) {
                int num = 0;
                for (int j = 0; j < n; j++) {
                    // compare birthdates to dates
                    if (birthdate[j] == i) {
                        num++;
                    }
                }
                if (num > 1) {
                    foundDup = true;
                }
                num = 0;
            }

            // count cases with duplicates
            if (foundDup) {
                dups++;
            }
        }

        double ans = (double) dups / numExperiments;
        System.out.println("The answer is " + ans);
    }
}
于 2015-09-20T00:59:01.820 回答