tags:

views:

22

answers:

0

I am always confused about how to write the code about combination related problem, including combination, permutations and subset problem.

There are three requests for such problems

1 just need a number of the results, for example, the number of the combination of 3 char out of 5 is 10, the number of permutation of 3 out of 5 is 60.

2 all the results are required. For example, all the result for the combination of 3 char out of 5 is

combination: ABC, ABD, ABE, ......and etc

permutation: ABC, ACB, BAC,BCA,CAB,CBA,....and etc.

3 Randomly generate one of results with the same probability.

My question is about the way to write code for different problems.

For the case 1, in which only the number is needed, it seems easy.

However, in the case 2, how can I store all the results and print them out when the recursive function is called? I am really puzzled about the case 2. Could any body give some some samples? I have no idea about the case 3. I am so grateful if anybody could help me out.

I am not asking about the algorithm only but how to write the neat code.

Thanks!