fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <string>
  5.  
  6. int color_of_card(int i)
  7. {
  8. return i / 13;
  9. }
  10.  
  11. int value_of_card(int i)
  12. {
  13. return i % 13;
  14. }
  15.  
  16. std::string card_as_string(int i)
  17. {
  18. static const std::string facevalues[] = {
  19. "Two", "Three", "Four", "Five", "Six", "Seven",
  20. "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
  21. };
  22. static const std::string suits[] = { "Diamonds", "Hearts", "Spades", "Clubs" };
  23.  
  24. return facevalues[value_of_card(i)] + " of " + suits[color_of_card(i)];
  25. }
  26.  
  27. int getcard() {
  28. return rand() % 52;
  29. }
  30.  
  31. int main() {
  32. const int times = 1000;
  33.  
  34. int counter = 0;
  35. for (int y = 0; y != times; y++) {
  36. const auto card1 = getcard();
  37. auto card2 = getcard();
  38. while (card1 == card2) { card2 = getcard(); } // Ensure cards differ.
  39.  
  40. if (value_of_card(card1) == value_of_card(card2)) {
  41. ++counter;
  42. }
  43. }
  44. std::cout << counter << std::endl; // 58 or 59 normally
  45. // Once you took a card, there are only 3 card on same value
  46. // and there is 51 remaining cards.
  47. std::cout << 3 / 51.f << std::endl; // 0.0588235
  48. }
  49.  
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
58
0.0588235