我有一个关于找到N人生日相同的事件的家庭作业。我必须编写一个函数findSameBirthday,它将birthdays数组作为参数
如果K出现类似,则函数返回true
N个元素中的生日。我已经生成了一个名为birthdays
的数组,它填充了从0到364的数字。但是,当我运行我的代码时,我似乎没有得到输出。我哪里出错?
我想在数组中检查相同的生日是:
birthdays[0]
开始,我会与birthdays[1]
,birthdays[2]
,birthdays[3]
,...,birthdays[999]
进行核对。birthdays[1]
开始,我会使用birthdays[2]
,...,birthdays[999]
进行检查。hit
增加1。hit
为1或更多,我就会返回true
,否则我会从函数false
返回findSameBirthday
。这是我的代码;它也在这里:http://codepad.org/JFLvUn4w
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define SEED time(0)
#define MAX 1000
void fillBirthdays(int birthdays[], int N);
bool findSameBirthday(int birthdays[], int k);
int main(void)
{
int birthdays[MAX]={0};
int hits=0, k=0, N=0;
scanf("%d", &N);
srand((unsigned int) SEED);
fillBirthdays(birthdays, N);
findSameBirthday(birthdays, k);
printf("%d", hits);
return 0;
}
/*
fillBirthdays fills N birthdays into the array birthdays
by using a table-lookup method. The indices 0 to 364
represent the different birthdays, and the value of
birthdays[k] is the number of people within the group of N
having the same birthday k.
Precondition: none.
*/
void fillBirthdays(int birthdays[], int N)
{
int i=0;
for(i=0;i<N;i++)
{
birthdays[i]=rand()%365;
}
return;
}
/*
findSameBirthday returns true if there are k (or more)
same birthdays, and false otherwise.
Precondition: none.
*/
bool findSameBirthday(int birthdays[], int k)
{
int N=0, i=0, j=0, hits=0;
bool samebirthday=true;
for(i=0;i<N;i++)
{
for(j=1;j<N;j++)
{
if(birthdays[i]==birthdays[j])
hits=hits+1;
}
}
if(hits>0)
return samebirthday;
else
return false;
}
答案 0 :(得分:1)
这应修复您的findSameBirthday
功能:
/*
findSameBirthday returns true if there are k (or more)
same birthdays, and false otherwise.
Precondition: none.
*/
bool findSameBirthday(int birthdays[], int k)
{
int hits=0;
/*we don't know how big the array is, but that's ok,
loop to max but break out early if needed.
birthdays array is 0 initialised so will be filled
with 0's where no data is present. */
for(int i=0; i < MAX; i++)
{
if (!birthdays[i]) {break;} //we have reached the end of the array, break out
//and check hit count
for(int j = i+1; j < MAX; j++) //start from i+1 so there is no overlap
{
if (!birthdays[j]) {break;} //we have reached the end of the array, break out
//and check next number
if(birthdays[i] == birthdays[j])
{hits++;}
}
}
if(hits >= k) // >= means equal or more
{return true;}
else
{return false;}
}
您当然可以将N
传递给函数。然后你可以删除循环中的检查和中断,但我想匹配你的函数声明。
/*
findSameBirthday returns true if there are k (or more)
same birthdays, and false otherwise.
Precondition: none.
*/
bool findSameBirthday(int birthdays[], int N, int k)
{
int hits=0;
/*we don't know how big the array is, but that's ok,
loop to max but break out early if needed.
birthdays array is 0 initialised so will be filled
with 0's where no data is present. */
for(int i=0; i < n; i++)
{
for(int j = i+1; j < n; j++) //start from i+1 so there is no overlap
{
if(birthdays[i] == birthdays[j])
{hits++;}
}
}
if(hits >= k) // >= means equal or more
{return true;}
else
{return false;}
}
您可以通过以下输入理解:
int birthdays[] = {5, 5, 2, 7, 5, 9, 5};
此算法的结果为sum(0 to n-1)
,其中n
是分享同一个生日的人数?
(在这种情况下,结果将是6
)。
如果这不是您想要的行为,请注意我们将对此进行处理。
至于为什么你没有得到任何输出,你应该(目前)看到0
输出(see here)。这是因为您将main中的hits
设置为0
,并且不对其进行修改。
如果你想要它只是说有&#39; k&#39;是否匹配生日,您可以将main()
的结尾更改为:
//findSameBirthday(birthdays, k);
printf("%s", findSameBirthday(birthdays, k)?
"No. of matching Birthdays hits threshold":
"No. of matching Birthdays does not hit threshold"
);
如果要输出hits
的数量:
findSameBirthday
的功能原型更改为int findSameBirthday(...)
hits
findSameBirthday()
主要:
hits = findSameBirthday(birthdays,k); printf(&#34;找到匹配的生日数量:%d&#34;,命中);
这将输出命中数。