在我详细了解代码之前,让我解释一下这个问题。我使用Microsoft Visual Studio 2013编译和运行此代码。当我从计算概率的函数中为任何行添加书签时,此代码执行完美。但是,当我没有为任何行添加书签或为主函数中的某些行添加书签时,程序无法正常工作。通过不工作我的意思是无论我运行多少次,它都会将概率设为100%或0%。答案是50%,因此概率应该接近50%。 (当我模拟它300次,同时书签它给了42%)似乎这个问题与内存有关。但我只是一名CS学生,所以我真的只是猜测这一点。任何类型的反馈或解决方案都表示赞赏。
让我解释代码假设要做什么。我的朋友最近问我一个概率问题。我无法使用某种类型的逻辑解决它,这使得它很容易解决。所以我决定制作一个小程序,通过模拟给定的时间来计算它的几率。所以这就是问题。
"飞机上有100个座位。第一位乘客丢失了机票。他不知道哪个座位是他的。所以他随意坐着。这个人一个接一个地来到飞机后的每个人。如果他们的座位是空的,他们就坐在指定的座位上。如果他们的座位被占用,他们随意坐着。最后一个人坐在指定座位上的概率是多少?"
以下是所有代码及其工作原理的评论。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string.h>
#include <string>
#define PlaneCapacity 100 //Plane capacity is now changeable. It helped me discover that this number is irrelevant to the answer of the question
using namespace std;
int PlacePeople(int SimNumber);
int SimNumber;
int Seat;
int PersonNo;
int SuccessNo = 0;
int Seats[PlaneCapacity];
int LatestSeat;
int main(){
double Probability;
cout << "Please enter the number of times you want the simulation to run:" << endl;
cin >> SimNumber;
PlacePeople(SimNumber); // Function that does the calculation
Probability = (SuccessNo * 100) / (SimNumber); //Calculate the probability in precentage form
cout << "Probability: ";
cout << Probability << endl;
system("pause");
return EXIT_SUCCESS; //Exit with success...I wish :(
}
int PlacePeople(int SimNumber){
for (int a = 0; a < SimNumber; a++){ //Number of simulations. The code doesn't work when I bookmark this line
Seats[PlaneCapacity] = { 0 }; //Array with elements that corresponds to the plane seats. Code works when I bookmark any line from this point onward. This line included.
PersonNo = 1; //Last person that sit down
LatestSeat = 0; //The seat the last person sit on
srand(time(NULL));
Seat = rand() % PlaneCapacity + 1; //Randomizer that determines where the first passanger will sit
if (Seat == 1){ //If he sits in his place everything is golden!
SuccessNo++;
}
if (1 < Seat && Seat < PlaneCapacity){ //If he doesn't sit in his place or the last passengers place things gets a bit messy
Seats[Seat - 1] = PersonNo; //Put him to his seat e.g 45th seat
for (int b = 1; b < Seat - 1; b++){ //Everybody until that seat (43 people, from 2 to 44) sits in their regular place. 45th person has no where to sit :(
PersonNo++;
Seats[PersonNo - 1] = PersonNo;
}
PersonNo++;
LatestSeat = PersonNo;
while (PersonNo < PlaneCapacity){ //The same process for the first passanger will be repeated until the last person is seated
Seat = rand() % (PlaneCapacity - LatestSeat + 1) + LatestSeat; //I tried to lower the random number interval so the code would work a little more efficiently
if (Seat == LatestSeat){ //The first guys seat might still be empty. So my interval is 1 bigger than it should be. Normally
SuccessNo++; //i would just place him when the random number says 1. But to make the interval shorter I now place the
break; //next guy to the first seat when the random number generator gives the latest seat number
} //So in my example if the random number is 45 I place the 45th guy to the 1st seat. Once the first seat is occupied
Seats[Seat - 1] = PersonNo; //We are guarenteed to have the last guy sit in his place so code can exit after that and increase the success counter
for (int b = LatestSeat; b < Seat - 1; b++){
PersonNo++;
Seats[PersonNo - 1] = PersonNo;
}
PersonNo++;
LatestSeat = PersonNo;
}
}
}
return SuccessNo; //return the number of succesfull attepmts to the main function
}
谢谢你的时间!
答案 0 :(得分:1)
要跟进我的评论,请在调用PlacePeople()之前将srand调用移至main。当你没有进入调试器时,我认为该函数执行得足够快,所以外观的所有迭代都得到相同的随机数。要验证这一点,请尝试打印座位值,看看它是否随机分配在可用座位上。