一道概率题 疯子坐飞机问题
来自博客园的一道概率题 感觉蛮有趣味的
出题的博主提供的解法看似巧妙其实缺乏说服性或者根本就是错误的。
其实用概率推导还是可以比较容易找到规律,简证如下:
2号坐对的概率是99/100
至于3号做错的概率是 = 1/100 {一号坐在三号上}+ 1/100{二号坐错了} X 1/99 {二号坐在三号上} = 1/99
所以3号坐对的概率是1 - 1/99 = 98/99
依次可以证明4号 = 1-(1/100 {一号坐在四号上}+ 1/100{二号坐错了} X 1/99 {二号坐在四号上} + 1/99{三号 坐错了} X 1/98 {三号坐在三四上})= 97/98
。。。
99号 = 2/3
100号 = 1/2
以下是验证代码。可以修改参数测试某个座位的概率值,程序显示了理论值(第一行)和实验值(第二行)。
static void Main(string[] args)
{
int total_seats = 100; //总共多少座位
int which_seat = 100; //哪个座位?从1算起
int test_count = 10000;
int true_value = 0;
for (int i = 0; i < test_count; i++)
{
if (isRightSeat(total_seats, which_seat))
true_value++;
}
Console.WriteLine((double)(100 - which_seat + 1) / (double)(100 - which_seat + 2));
Console.WriteLine(true_value / (double)test_count);
Console.ReadKey();
}
static Random rnd = new Random();
static bool isRightSeat(int total, int pos)
{
int[] seats = new int[total];
List<int> positions = Enumerable.Range(0, total).ToList();
int n = pos;
for (int i = 0; i < n; i++)
{
if (i == 0 || seats[i] != 0)
{
int p = rnd.Next(positions.Count);
int next_seat = positions[p];
positions.RemoveAt(p);
seats[next_seat] = i + 1;
}
else
{
seats[i] = i + 1;
positions.Remove(i);
}
}
return seats[pos - 1] == pos;
}
{
int total_seats = 100; //总共多少座位
int which_seat = 100; //哪个座位?从1算起
int test_count = 10000;
int true_value = 0;
for (int i = 0; i < test_count; i++)
{
if (isRightSeat(total_seats, which_seat))
true_value++;
}
Console.WriteLine((double)(100 - which_seat + 1) / (double)(100 - which_seat + 2));
Console.WriteLine(true_value / (double)test_count);
Console.ReadKey();
}
static Random rnd = new Random();
static bool isRightSeat(int total, int pos)
{
int[] seats = new int[total];
List<int> positions = Enumerable.Range(0, total).ToList();
int n = pos;
for (int i = 0; i < n; i++)
{
if (i == 0 || seats[i] != 0)
{
int p = rnd.Next(positions.Count);
int next_seat = positions[p];
positions.RemoveAt(p);
seats[next_seat] = i + 1;
}
else
{
seats[i] = i + 1;
positions.Remove(i);
}
}
return seats[pos - 1] == pos;
}