找出发生概率N个人生日相同

时间:2014-10-14 12:19:00

标签: c arrays random

我有一个关于找到N人生日相同的事件的家庭作业。我必须编写一个函数findSameBirthday,它将birthdays数组作为参数 如果K出现类似,则函数返回true N个元素中的生日。我已经生成了一个名为birthdays的数组,它填充了从0到364的数字。但是,当我运行我的代码时,我似乎没有得到输出。我哪里出错?

我想在数组中检查相同的生日是:

  1. birthdays[0]开始,我会与birthdays[1]birthdays[2]birthdays[3],...,birthdays[999]进行核对。
  2. birthdays[1]开始,我会使用birthdays[2],...,birthdays[999]进行检查。
  3. 对于每次成功的查找,我会将hit增加1。
  4. 因此,只要hit为1或更多,我就会返回true,否则我会从函数false返回findSameBirthday
  5. 这是我的代码;它也在这里: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;
      }
    

1 个答案:

答案 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;,命中);

这将输出命中数。