fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6. string s = "00000000001111111111"; // red and white roses
  7. sort(s.begin(), s.end());
  8.  
  9. int total_permutations = 0;
  10. int count_in_church = 0;
  11.  
  12.  
  13. // check next lexicographic permutation of s
  14. do {
  15. total_permutations += 1;
  16.  
  17. // check if bride steps into church by checking if
  18. // the number of ones exceeds the number of zeros
  19. int cnt_0 = 0;
  20. int cnt_1 = 0;
  21.  
  22. for (char c : s) {
  23. if (c == '0') {cnt_0 += 1;}
  24. else {cnt_1 += 1;}
  25.  
  26. if (cnt_1 > cnt_0) {
  27. count_in_church += 1;
  28. break;
  29. }
  30. }
  31.  
  32. } while(std::next_permutation(s.begin(), s.end()));
  33.  
  34.  
  35. cout << "number of times bride entered church: " << count_in_church << '\n';
  36. cout << "total permutations: " << total_permutations << '\n';
  37. cout << "probability: " << 1.0 * count_in_church / total_permutations << '\n';
  38. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
number of times bride entered church: 167960
total permutations: 184756
probability: 0.909091