fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int count[19] = {0};
  5. void diceSumHelper(int n, int currentSum, std::vector<int>& path) {
  6. if (n == 0) {
  7.  
  8. // Print the path (combination of dice rolls)
  9. // for (int roll : path) {
  10. // std::cout << roll << " ";
  11. // }
  12. // std::cout << std::endl;
  13. count[currentSum] ++;
  14. return;
  15. }
  16.  
  17. // Roll the dice from 1 to 6
  18. for (int i = 1; i <= 6; ++i) {
  19. path.push_back(i);
  20. diceSumHelper(n - 1, currentSum + i, path);
  21. path.pop_back();
  22. }
  23. }
  24.  
  25. void diceSum(int n) {
  26. std::vector<int> path;
  27. diceSumHelper(n, 0, path);
  28. }
  29.  
  30. int main() {
  31. int numDice = 3;
  32. diceSum(numDice);
  33.  
  34. for (int c = 0; c <= 18; c++) {
  35. std::cout << count[c] << " ";
  36. }
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
0 0 0 1 3 6 10 15 21 25 27 27 25 21 15 10 6 3 1