Answer to Question #62750 in C++ for Hope

Question #62750
Write a C++ program to implement an algorithm that generates the factorial representation of a natural number k. and generates the permutation of rank k in the lexicographic order of the permutations of an n-set. You must first prompt the user for the value of n, then for value(s) of k. Remember to use the alphabet for the elements of your permutation.

As an example, the factoradic number 2110_! is equal to

2 x 3! + 1 x 2! + 1 x 1! + 0 x 0!
= 2 x 6 + 1 x 2 + 1 x 1 + 0 x 1
= 12 + 2 + 1 + 0
= 15
Eg string like {A…..Z}.. A[26]… n= index; so if n=4 is n= {A,B,C} and n =4 {A,C,B,D}…..
1
Expert's answer
2016-10-24T07:33:08-0400
#include <iostream>
#include <string>
#include <cstdlib>


int factorial(int a)
{
int s = 1;
for (int i = 1; i <= a; ++i)
{
s = s * i;
}

return s;
}

void solve(const std::string &number)
{
int pos = number.size() - 1;
int sum = 0;

for (int i = 0; i < number.size(); ++i)
{
sum = sum + factorial(pos) * (number[i] - '0');
pos--;
}
std::cout << sum << std::endl;
}

int main()
{
std::string number = "";
std::cin >> number;

solve(number);

system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>


void show(char mass[], int n)
{
for (int i = 0; i < n; ++i)
{
std::cout << mass[i];
}
std::cout << std::endl;
}


void solve(char mass[], int n)
{
int code = 65;
for (int i = 0; i < n; ++i)
{
mass[i] = code;
code++;
}
}

int main()
{
char mass[26] = {0};
int n = 0;
std::cin >> n;

solve(mass, n);

show(mass, n);


system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS