fork(1) download
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. void getPermutations(int index, vector<int> output, vector< vector<int> > &solutions, vector<int> boxes)
  6. {
  7. if (index == output.size())
  8. {
  9. solutions.push_back(output);
  10. return;
  11. }
  12. vector<int>::iterator it;
  13. for (it = boxes.begin(); it != boxes.end(); it ++)
  14. {
  15. #ifdef _DEBUG
  16. printf("index= %d\n",index);
  17. printf("*it= %d",*it);
  18. #endif
  19. output[index] = (*it);
  20. getPermutations(index+1,output,solutions, boxes);
  21. }
  22.  
  23. }
  24.  
  25. int main()
  26. {
  27.  
  28. vector< vector<int> > solutions;
  29.  
  30. int nbval=2; // 0 1 ... n
  31. vector<int> boxes;
  32. for (int i=0; i<nbval; i++)
  33. boxes.push_back(i);
  34.  
  35. int nbs = 4;
  36. vector<int> outvec;
  37. outvec.assign(nbs,0);
  38.  
  39. // recursive call
  40. getPermutations(0, outvec, solutions, boxes);
  41.  
  42. // print solutions
  43. for (int i=0; i<solutions.size();i++)
  44. {
  45. cout << endl;
  46. for(int j =0;j<solutions[i].size();j++)
  47. cout << solutions[i][j] << " " ;
  48. }
  49.  
  50. printf("\n\nNumber of permutations with repetitions = %d\n\n",solutions.size());
  51.  
  52. //system("pause");
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
0  0  0  0  
0  0  0  1  
0  0  1  0  
0  0  1  1  
0  1  0  0  
0  1  0  1  
0  1  1  0  
0  1  1  1  
1  0  0  0  
1  0  0  1  
1  0  1  0  
1  0  1  1  
1  1  0  0  
1  1  0  1  
1  1  1  0  
1  1  1  1  

Number of permutations with repetitions = 16