fork download
  1. #include <stdio.h>
  2.  
  3. enum PokerRank {
  4. CARD5, CARD4, CARD3, PAIR2, PAIR1, PAIR0,
  5. STRAIGHT, FULLHOUSE,
  6. MAXPOKERRANK,
  7. };
  8.  
  9. int CheckRank(int *d){
  10. int p[7] = {0};
  11. int i, j;
  12. int tmp;
  13.  
  14. for (i = 0; i < 5; i++) {
  15. p[d[i]]++;
  16. }
  17.  
  18. if (p[1] && p[2] && p[3] && p[4] && p[5]) return STRAIGHT;
  19. if (p[2] && p[3] && p[4] && p[5] && p[6]) return STRAIGHT;
  20.  
  21. // sort
  22. for (i = 0; i < 6; i++) {
  23. for (j = i + 1; j < 7; j++) {
  24. if (p[i] < p[j]) {
  25. tmp = p[j];
  26. p[j] = p[i];
  27. p[i] = tmp;
  28. }
  29. }
  30. }
  31.  
  32. if (p[0] == 5) return CARD5; // 5 card
  33. if (p[0] == 4) return CARD4; // 4 card
  34. if ((p[0] == 3) && (p[1] == 2)) return FULLHOUSE; // Full House
  35. if (p[0] == 3) return CARD3; // 3 card
  36. if ((p[0] == 2) && (p[1] == 2)) return PAIR2; // 2 Pair
  37. if (p[0] == 2) return PAIR1; // 1 Pair
  38. return PAIR0; // Buta (No Pair)
  39. }
  40.  
  41. void DicePoker(void) {
  42. int d[5] = {1, 1, 1, 1, 1};
  43. int r[MAXPOKERRANK] = {0};
  44. int i;
  45. int loop = 1;
  46. int count = 0;
  47.  
  48. while (loop) {
  49. r[CheckRank(d)]++;
  50. count++;
  51.  
  52. d[0]++;
  53. for (i = 0; i < 5; i++) {
  54. if (d[i] > 6) {
  55. if (i == 4) {
  56. loop = 0;
  57. break;
  58. }
  59. d[i] = 1;
  60. d[i + 1]++;
  61. }
  62. }
  63. }
  64.  
  65. printf("5 dice poker rate :\n");
  66. printf("------------------------------------\n");
  67.  
  68. printf("5 Card = %4d/%d (%.2f%%)\n",
  69. r[CARD5], count, (float)r[CARD5]*100/count);
  70. printf("4 Card = %4d/%d (%.2f%%)\n",
  71. r[CARD4], count, (float)r[CARD4]*100/count);
  72. printf("Straight = %4d/%d (%.2f%%)\n",
  73. r[STRAIGHT], count, (float)r[STRAIGHT]*100/count);
  74. printf("Full House = %4d/%d (%.2f%%)\n",
  75. r[FULLHOUSE], count, (float)r[FULLHOUSE]*100/count);
  76. printf("3 Card = %4d/%d (%.2f%%)\n",
  77. r[CARD3], count, (float)r[CARD3]*100/count);
  78. printf("2 Pair = %4d/%d (%.2f%%)\n",
  79. r[PAIR2], count, (float)r[PAIR2]*100/count);
  80. printf("1 Pair = %4d/%d (%.2f%%)\n",
  81. r[PAIR1], count, (float)r[PAIR1]*100/count);
  82. printf("No Pair = %4d/%d (%.2f%%)\n",
  83. r[PAIR0], count, (float)r[PAIR0]*100/count);
  84. }
  85.  
  86. int main(void) {
  87. DicePoker();
  88. return 0;
  89. }
  90.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
5 dice poker rate :
------------------------------------
5 Card     =    6/7776 (0.08%)
4 Card     =  150/7776 (1.93%)
Straight   =  240/7776 (3.09%)
Full House =  300/7776 (3.86%)
3 Card     = 1200/7776 (15.43%)
2 Pair     = 1800/7776 (23.15%)
1 Pair     = 3600/7776 (46.30%)
No Pair    =  480/7776 (6.17%)