fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <random>
  4.  
  5. using namespace std;
  6.  
  7. class Rand
  8. {
  9. public:
  10. Rand(bool rand_seed = true, int value = 0)
  11. {
  12. if(rand_seed) u.seed(std::random_device()());
  13. else u.seed(value);
  14. }
  15. int operator()(int from, int to)
  16. {
  17. return d(u,std::uniform_int_distribution<>::param_type{from,to});
  18. }
  19. private:
  20. std::default_random_engine u;
  21. std::uniform_int_distribution<> d;
  22. } r;
  23.  
  24. int counts()
  25. {
  26. int s_1[2] = {2,3};
  27. int s_2[4] = {1,3,6,4};
  28. int s_3[4] = {1,2,6,5};
  29. int s_4[3] = {2,6,5};
  30. int s_5[3] = {3,6,4};
  31.  
  32. int cnt = 0, s = 1;
  33. for(;s != 6; ++cnt)
  34. {
  35. switch(s)
  36. {
  37. case 1: s = s_1[r(0,1)]; break;
  38. case 2: s = s_2[r(0,3)]; break;
  39. case 3: s = s_3[r(0,3)]; break;
  40. case 4: s = s_4[r(0,2)]; break;
  41. case 5: s = s_5[r(0,2)]; break;
  42. }
  43. }
  44. return cnt;
  45. }
  46.  
  47. int main()
  48. {
  49. int tot = 0, ok = 0;
  50. for(; tot < 10000000;)
  51. {
  52. ++tot;
  53. ok += counts();
  54. }
  55. cout << double(ok)/tot << "\n";
  56. }
  57.  
Success #stdin #stdout 1.94s 4888KB
stdin
Standard input is empty
stdout
5.33419