c++生日悖论程序
C++ Birthday Paradox Program
我正试图为我的c++类找出生日悖论程序。这是我目前的方法,但行不通。我试着在这里寻找关于同一主题的其他问题,但我仍然很迷路,所以任何帮助都会非常感激。
//This program calculates the likelihood of any two people in groups of 2-50 people
//having their birthday on the same day, also known as the Birthday Paradox.
#include <iostream>
using namespace std;
int main()
{
int people, trial, count = 0, birthdays[50], numMatches, NUM_TRIALS = 5000;
double total;
//function call
sameBirthday(birthdays, people);
numMatches = 0;
for (people = 2; people <= 50; people++)
{
//Run trials to see if people have the same birthday
//Reset number of matches
numMatches = 0;
for (trial = 0; trial < NUM_TRIALS; trial++)
{
//Randomly generate up to "people" birthdays
for (int i = 0; i < people, i++)
{
birthdays[people] = (rand() % 365) + 1;
//Check to see if any two people have the same birthday
for (i = 1; i < people; i++)
{
//birthday one
for (int j = 0; j < i-1; j++)
{
//birthday two
for (int k = j +1; k < i; k++);
}
}
}
}
}
bool sameBirthday(int birthdays[], int people)
{
//if the two birthdays are the same, add one to the count
if (birthdays[j] == birthdays[k])
{
people++;
}
total = (numMatches / 5000.0);
cout << "For " << people << " people, the probability of two birthdays is about " << total << endl;
}
return 0;
for (people = 2; people <= 50; people++)
{
//Run trials to see if people have the same birthday
//Reset number of matches
numMatches = 0;
for (trial = 0; trial < NUM_TRIALS; trial++)
{
//Randomly generate up to "people" birthdays
for (int i = 0; i < people, i++)
{
//here I use i instead of people so every time i put the new number in a different position
birthdays[i] = (rand() % 365) + 1;
//this loop check if some birthday is equal to the one just generated
for(int j = 0; j < i; j++){
if(birthday[j] == birthday[i]){
//here do what u want to do when 2 people have the same birthday
}
}
}
}
}
尝试使用这个循环,通过这种方式,如果有两个人生日相同,则检查从2到50的每个数字。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- c++生日悖论程序