fork(2) download
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. int main() {
  7. int N=3000000;
  8. int totalwinnings=0;
  9. srand (time(NULL));
  10. for(int j=0; j<N; j++){ // plays the game N times
  11. bool deck[52] = {false};
  12. int pick;
  13. bool filled = 0;
  14. int red = 26;
  15. int black = 26;
  16. int wallet = 0;
  17.  
  18. for(int i=0; i<26; i++){ // randomises the deck
  19. filled=0;
  20. while(!filled){
  21. pick=rand()%52;
  22. if(deck[pick]==0){deck[pick]=1; filled=1;}
  23. }
  24. }
  25. for(int i = 0; i<52; i++){ // plays the game with smart strategy
  26.  
  27. if(red<black){if(deck[i]==0){wallet++;} else{wallet--;}}
  28. else{if(deck[i]==1){wallet++;} else{wallet--;}}
  29.  
  30. black -= 1-deck[i]; red -= deck[i];
  31.  
  32. // cout << "Deck: "; for(int i=0; i<52; i++){cout << deck[i];}
  33. // cout << " Reds: " << red << " Blacks: " << black;
  34. // cout << " Wallet: " << wallet << endl;;
  35.  
  36. }
  37. totalwinnings += wallet;
  38. }
  39. cout << "Total Winnings: " << totalwinnings << " Average Winnings: " << float(totalwinnings)/N;
  40. return 0;
  41. }
Success #stdin #stdout 4.12s 3140KB
stdin
Standard input is empty
stdout
Total Winnings: 24250240 Average Winnings: 8.08341