Advertisement
Guest User

Birthdays (Aliens Question)

a guest
Sep 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. double get_birthdays(int n);
  7.  
  8. int cmpfunc (const void * a, const void * b);
  9.  
  10. int main(int argc, char **argv) {
  11.   for(int n = 366; n < 500; n++){
  12.     printf("%f\n", get_birthdays(n));
  13.   }
  14. }
  15.  
  16. int cmpfunc (const void * a, const void * b) {
  17.    return ( *(int*)a - *(int*)b );
  18. }
  19.  
  20. double get_birthdays(int n) {
  21.   srand(time(NULL));
  22.   int birthdays[25];
  23.   int b;
  24.   int count = 0;
  25.   for(int i = 0; i < 10000; i++){
  26.     b = 0;
  27.     for(int j = 0; j < 25; j++){
  28.       birthdays[j] = rand() % n;
  29.     }
  30.     qsort(birthdays, 25, sizeof(int), cmpfunc);
  31.     for(int j = 0; j < 24; j++){
  32.       if(birthdays[j] == birthdays[j+1]){
  33.         b = 1;
  34.       }
  35.     }
  36.     if(b){
  37.       count++;
  38.     }
  39.   }
  40.   return ((double) count)/10000.0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement