- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个任务,我需要在多次试验 (5000) 中计算给定房间大小(在我的例子中是 50)中两个人生日相同的概率。我必须将生日随机分配给房间里的人数。区别在于我需要使用 boolean 函数来检查生日是否相同。我不明白为什么我的输出关闭,但我相信这与我的两个循环有关。
>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
bool SameBirthday(int birthdays[], int numpeople);
const int MAX_PEOPLE = 50;
const double NUM_TRIALS = 5000.0;
const int DAYS_IN_YEAR = 365;
int main(void)
{
int numMatches = 0;
int people = 2;
int trial = 0;
int numpeople = 0;
int i = 0;
int birthdays[MAX_PEOPLE];
bool Match;
double Probability = 0;
srand(time(0));
for (people = 2; people <= MAX_PEOPLE; people++)
{
numMatches = 0;
for (trial = 0; trial < NUM_TRIALS; trial++)
{
for (i = 0; i < people; i++)
{
birthdays[i] = (rand() % 365 + 1);
numpeople = i;
}
if ((SameBirthday(birthdays, numpeople) == true))
{
numMatches++;
}
}
Probability = (numMatches / NUM_TRIALS);
cout << "For " << people << ", the probability of two birthdays is about " << Probability << endl;
}
}
bool SameBirthday(int birthdays[], int numpeople)
{
bool match = false;
int numberofmatches = 0;
//Use this function to attempt to search the giving array birthdays and count up number of times
//at least two people have matching birthdays for any given 1 trial
for (int SpaceOne = 0; SpaceOne < numpeople; SpaceOne++)
{
for (int SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)
{
if (birthdays[SpaceTwo] == birthdays[SpaceOne])
{
return true;
}
}
}
return false;
}
I know that the code has errors in certain spots that was because I started trying different things, but any help would be appreciated.
EDIT- My only issue now is that for my output I have a zero for the probability of 2 people in the room have a birthday, which is not right. It seems like my outputs are like a person off, the probability of 2 people is shown as the probability for three people and so on.
EDIT(8-31-2015): I also forgot to mention that my Professor stated that my SameBirthday function needed the parameters: birthday[], and numpeople so I cannot use MAX_PEOPLE as a parameter. My professor also suggested using a triple nested for loop within the main body of the function. I believe what is making my output off by one for each person relates to the triple nested for loop, but I am unsure what would cause the issue.
最佳答案
就这样吧:
bool SameBirthday(int birthdays[], int numPeople)
{
for(int x=0; x<numPeople; x++){
for(int y=0; y<numPeople; y++){
if(birthdays[x] == birthdays[y])
return true;
}
}
return false;
}
你在嵌套循环中的逻辑是错误的..
for (SpaceOne = 0; SpaceOne < numpeople - 1; SpaceOne++)
for (SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)
您的内部循环正在跳过 n
次检查,其中 n
等于 SpaceOne。
顺便说一句,这不是 C 编程。您可以在 for 循环中声明变量。
关于使用 boolean 函数的 C++ 生日悖论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289294/
如果我在 forms.py 中有: birthdate = forms.DateTimeField() 和 html: 我需要创建一个新的小部件还是有答案?
我正在尝试从 facebook sdk 获取生日,我可以获取我的生日,但我无法从其他帐户获取生日。我已经在“设置”中设置了联系人电子邮件,并在此处回答了一个应用程序供公众使用:Set up app p
我需要存储用户的出生日期。我在前端创建了一个表单,通过 3 个选择下拉菜单获取他们的出生日期年、月和日。我目前将这三个值存储在 SQL 中,并具有相同名称的列。 我需要执行的查询之一是获取高于或低于特
我有 mysql 数据库,以生日列作为日期。 保存到此列的最佳方式是什么,年份是可选的。我应该随便放一些年份吗?在数据库中保存生日的最佳做法是什么。 我在 cakephp 中创建了 3 个文本字段,日
考虑到此代码(基于 Python/Mongoengine),我知道如何找到下一个即将到来的生日 class User(mongoengine.Document): (...) birthday
在以前版本的 Prestashop 1.6 中,注册表单有生日字段的日期选择器和一个下拉列表,我们可以在其中选择月、日、年。 Prestashop 版本 1.6 - 生日截图: 然后在最新版本的Pre
我正在使用带有changeYear 的jquery ui datepicker。问题是它以 block 的形式显示年份(从 1985 年到 2005 年,然后单击 1985 年打开其他年份)我需要显示
我正试图在来自佛蒙特州和纽约的成员(member)的生日表中找到最年长的人。我的成员类似于以下内容: Members ------- MemberID, Firstname, Lastname, Bi
工作中需要用到身份证验证,还要支持检查15位和18位。 我一时手懒,问同事有没有现成的函数可用,同事google了一下,扔给我一个 asp-vbscript版本的函数。 可我这边是客户端j
我正在努力解决以下正则表达式: \b[\dBb][-. \dEe]+(\d{4})\b 它应该匹配帐号,但不匹配 token /生日或其中包含“be”的文本。在 community 的支持下,我成功捕
我有一个简单的小部件,它有: 请注意,今天是生日。因此,它意味着保持相同,无论您身在何处(不应该发生时区)。如果您 1 月 10 日凌晨 3:00 出生在英国,并且在纽约查看您的个人信息,您应该仍然
在我的数据库中,日期存储为 dd/mm/yyyy。当我检索它时,我想将日期分别拆分为字符串日、字符串月和字符串年。我应该怎么做? “/”将是分隔符,对吗? 最佳答案 您需要可以使用DateTime.P
我很难通过 Facebook SDK 获取用户信息。 我正在努力争取生日、工作和教育。 我正在使用这个: let graphRequest : FBSDKGraphRequest = FBSDKGr
我正在尝试使用 NSSortDescriptor 按日期对 NSMutableArray *friends, key:birthday 进行排序,我的 NSLog 中的所有生日都为空,但名称仍在记录中
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试从其 Google Plus 帐户获取用户的信息,包括性别和年龄。由于这些字段可能是私有(private)的,我认为明确请求它们可以解决问题。然而,尽管登录对话框明确指出该应用程序请求查看您
我正在制作一个 facebook 应用程序来显示用户的生日......... 'YOUR_APP_ID', 'secret' => 'Y
我正在尝试将 OAuth2 登录添加到我基于 Node/ExpressJS/MongoDB/PassportJS 构建的应用中。 我用这个方法可以成功登录,但是我无法获取某些信息。我唯一能够访问的是
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我无法让我的代码来获取我的测试帐户的“生日”,甚至我自己的“生日”(任何地方都没有成功!)来回显,尽管从我所看到的来看,我正在正确地请求它并且我拥有适当的权限对于“user_birthday”。 但是
我是一名优秀的程序员,十分优秀!