Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define SIZE 4
  4.  
  5. void printCombinationsWithRepetitions(int * arr, int * combs, int index,
  6. int sizeOfComb, int beg, int end)
  7. {
  8. if (index == sizeOfComb)
  9. {
  10. for (int i = 0; i < index; ++i)
  11. std::cout << arr[combs[i]];
  12.  
  13. std::cout << '\n';
  14. return;
  15. }
  16.  
  17. for (int j = beg; j <= end; ++j)
  18. {
  19. combs[index] = j;
  20. printCombinationsWithRepetitions(arr, combs, index + 1, sizeOfComb, j, end);
  21. }
  22. }
  23.  
  24. void combinationsWithRepetitions(int * arr, int size, int sizeOfComb)
  25. {
  26. int * temp = new int[sizeOfComb + 1];
  27. printCombinationsWithRepetitions(arr, temp, 0, sizeOfComb, 0, size - 1);
  28.  
  29. delete[] temp;
  30. }
  31.  
  32. int main()
  33. {
  34. int * arrayOfNumbers = new int[SIZE];
  35. for (int i = 0; i < SIZE; ++i)
  36. arrayOfNumbers[i] = i + 1;
  37.  
  38. int sizeOfComb = 2;
  39.  
  40. combinationsWithRepetitions(arrayOfNumbers, SIZE, sizeOfComb);
  41.  
  42. delete[] arrayOfNumbers;
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement