#include <iostream>
#include <vector>
using namespace std;
int factorial(int n)
{
if (n<2)
{
return 1;
}
else
{
return n*factorial(n - 1);
}
}
vector<int> insert(vector<int> arr, int pos, int val)
{
vector <int> temp(arr.size()+1);
for (int i = 0; i < pos; i++)
{
temp[i] = arr[i];
}
for (int i = arr.size(); i > pos; i--)
{
temp[i] = arr[i - 1];
}
temp[pos] = val;
return temp;
}
void disparr(int *arr, int sz)
{
for (int i = 0; i < sz; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
void disparr(vector<int> a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << ' ';
}
cout << endl;
}
vector<vector<int>> combmaker(vector<vector<int>> a, int elem)
{
vector<vector<int>> retval(factorial(a[0].size() + 1));
vector<int> b(a[0].size()+1,0);
for (int i = 0; i < retval.size(); i++)
{
retval[i] = b;
}
for (int i = 0; i < a[0].size() + 1; i++)
{
for (int j = 0; j < factorial(a[0].size()); j++)
{
retval[i * factorial(a[0].size()) + j] = insert(a[j], i, elem);
//disparr(temp4[i * factorial(4) + j], 5);
}
}
return retval;
}
void disparr2d(vector<vector<int>> a)
{
for (int i = 0; i < a.size(); i++)
{
for (int j = 0; j < a[i].size(); j++)
{
cout << a[i][j] << ' ';
}
cout << endl;
}
}
int main()
{
vector <int> a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; // set of numbers to make permutation
vector<vector <int>> b;
vector<vector<int>> init;
vector<int> inite = {a[0]};
init.push_back(inite);
b = combmaker(init, a[1]);
for (int i = 2; i < a.size(); i++)
{
b = combmaker(b, a[i]);
disparr2d(b);
cout << "finised " << i << " th iteration" << endl;
}
disparr2d(b);
return 0;
}
All possible permutation of a number set
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.