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