Skip to content

Instantly share code, notes, and snippets.

@rkhapov
Created January 6, 2016 17:31
Show Gist options
  • Save rkhapov/faa12e48335c20bf908f to your computer and use it in GitHub Desktop.
Save rkhapov/faa12e48335c20bf908f to your computer and use it in GitHub Desktop.
1
#include <iostream>
#include <cstdlib>
#include <string>
const std::string numbers = "123456789";
int summ = 0;
void permuntation(const std::string &source)
{
static std::size_t curr_index = 0;
static std::string curr = source;
if (curr_index == source.size() - 1)
{
summ += std::atoi(curr.c_str());
}
else
{
for (std::size_t i = curr_index; i < curr.size(); i++)
{
std::string temp = curr;
std::swap(curr[curr_index], curr[i]);
curr_index++;
permuntation(source);
curr_index--;
curr = temp;
}
}
}
int main()
{
std::size_t n;
std::cin >> n;
permuntation(numbers.substr(0, n));
std::cout << summ << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment