I am working on a Java assignment and I am absolutely stumped.
The question is:
Write a function using Recursion to do the following: You have X different cards. You have only Y envelopes. Y is less than or equal to X. For any given values of X and Y,
display all possible ways you can fill the Y envelopes when Order is not important and Repetition is not allowed.
hint: X! / (( X-Y)! * Y!)display all possible ways you can fill the Y envelopes when Order is important and Repetition is allowed
hint: X^Ydisplay all possible ways you can fill the Y envelopes when Order is important and Repetition is not allowed hint:
X! / (X – Y)!display all possible ways you can fill the Y envelopes when Order is not important and Repetition is allowed hint:
(X + Y – 1)! / (Y! * (X – 1)!)
for example, under case (1), if X = {J, Q, K, A) and Y = 3, then the output should be: {J,Q,K} {J,Q,A} {J,K,A} {Q,K,A}.
I do not want anyone to post any code and I'm not looking for anyone to solve this for me! I am hoping that once I get the first part (question a) done that it'll unlock the flood gates. Can someone please offer some guidance in working out the pseudocode algorithm, this is as far as I can get:
Fill the Y envelopes in order with increasing cards (ex: X=5, Y=3) {1, 2, 3}.
Replace in the highest envelope with the highest card {1, 2, 5}, decrementing until we find it's original value {1, 2, 4}.
Do this for every envelope from highest to lowest (where the number is not already in use) {1, 5, 4} {1, 3, 4} {5, 3, 4} {2, 3, 4}.
That's as far as I get before it falls apart because this is missing 3 combinations {1, 5, 3} {3, 4, 5} {5, 3, 2}.
I would appreciate any help at all and as it's an assignment I'll re-iterate, I don't want the solution, I want help in coming to the solution on my own. Thank you!
EDIT: I've tried all 3 solutions outlined and I'm still not getting it. This is what I'm getting so far:
public static void comboNoRep(String[] a, int y, boolean[] used)
{
if(y == 0) {
// found a valid solution.
System.out.println(result);
}
for(int i=0; i<a.length; i++) {
if(!used[i]) {
used[i] = true;
result = result + a[i];
comboNoRep(a, y - 1, used);
result = result + " ";
used[i] = false;
}
else {
}
}
}
Can anyone help point out my flaw?