Tuesday, July 28, 2009

Pls tell me how to write a function in C++ so that:?

I can get the permutations of some n numbers in a sequence.


and if i pass an integer k and the set on n numbers as an array, i will get the 'k'th permutation.i.e, if i pass the numbers 1 to n! along with the n-element array, in n! function calls, i must get the all n! possible permutations of the n numbers.





Please try to make the function so that it will work for any n.

Pls tell me how to write a function in C++ so that:?
either search for the keywords using some search engine... or, search in www.planetsourcecode.com; i have come across good codes (in the VB area; there must be in other areas as well) for exactly the same problem... the code is quite simple to implement, not complex as it seems....!
Reply:I don't know C++, but here's my suggestion:





Create a function between n factorial and all permutations as follows: Using loops within loops within loops, etc create the n factorial permuations. When each is created it is assigned the next number in the list. For example, to do this for all permutations of 1,2,3 do the following





k=1


loop a from 1 to 3


loop b from 1 to 3


loop c from 1 to 3





if a,b,c are all different then


f(k)=(a,b,c)


increment k


endif





increment c


increment b


increment a





The function f maps [1, n factorial] to all permutations.
Reply:Google it, keywords "C++ permutations sample"


No comments:

Post a Comment