fork(1) download
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4.  
  5. int cardnum[13];
  6.  
  7. void Initialize();
  8. int GetCard();
  9. int Loop();
  10. void ShowCards();
  11.  
  12. int main() {
  13. srand((unsigned)time(NULL));
  14. int loop = 10000000, lp = loop;
  15. int cnt = 0;
  16. while(lp--) {
  17. Initialize();
  18. if(Loop()) {
  19. ++cnt;
  20. //ShowCards();
  21. }
  22. }
  23. printf("%lf\n", (double)cnt / loop);
  24. }
  25.  
  26. void Initialize() {
  27. int i;
  28. for(i = 0; i < 13; ++i) cardnum[i] = 0;
  29. }
  30.  
  31. int GetCard() {
  32. int c = rand() % 13;
  33. if(cardnum[c] >= 4) {
  34. return GetCard();
  35. }
  36. return c;
  37. }
  38.  
  39. int Loop() {
  40. int i;
  41. for(i = 0; i < 14; ++i) {
  42. cardnum[GetCard()]++;
  43. }
  44. for(i = 0; i < 13; ++i) {
  45. if(cardnum[i] % 2) return 0;
  46. }
  47. return 1;
  48. }
  49.  
  50. void ShowCards() {
  51. int i;
  52. for(i = 0; i < 13; ++i) {
  53. int j;
  54. for(j = 0; j < cardnum[i]; ++j) {
  55. printf("%d ", i+1);
  56. }
  57. }
  58. printf("\n");
  59. }
Success #stdin #stdout 4.29s 2008KB
stdin
Standard input is empty
stdout
0.000738