fork download
  1. #include <iostream>
  2. #include <random>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. default_random_engine generator;
  8. uniform_int_distribution<int> distribution(0,5);
  9. int repeat = 3;
  10. int tot = 0;
  11. for(int itr = 0; itr<repeat; itr++)
  12. {
  13. int dice[5] = {0,0,0,0,0};
  14.  
  15. //first roll all the dice
  16. for(int i = 0; i<5; i++)
  17. {
  18. dice[i] = distribution(generator);
  19. }
  20.  
  21. //retry until all dice have the same outcome
  22. bool done = false;
  23. int totRolls = 1;//assuming fisrt roll counts..
  24. while(!done)
  25. {
  26.  
  27. //decide the most occurring outcome
  28. int cnt[6] = {0,0,0,0,0,0};
  29. int maxCnt = 0;
  30. for(int i = 0; i<5; i++)
  31. {
  32. cnt[dice[i]]++;
  33. if(cnt[dice[i]] > cnt[maxCnt])
  34. {
  35. maxCnt = dice[i];
  36. }
  37. if(cnt[dice[i]] >= 5)
  38. {
  39. done = true;
  40. break;
  41. }
  42. }
  43.  
  44. if(done)break;
  45. //now roll all the dice that have
  46. //different value than that..
  47. for(int i = 0; i<5; i++)
  48. {
  49. if(dice[i] != maxCnt)dice[i] = distribution(generator);
  50. }
  51.  
  52. totRolls++;
  53.  
  54. /*
  55.   //print all dice values
  56.   for(int i = 0; i<5; i++)
  57.   {
  58.   cout<<dice[i]+1<<" ";
  59.   }
  60.   cout<<endl;
  61.   */
  62. }
  63. // cout<<totRolls<<endl;
  64. tot+= totRolls;
  65. }
  66. cout<<tot/repeat<<endl;
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
11