Answer to Question #58434 in Java | JSP | JSF for soonh

Question #58434
How do I print all possible ways if I drop card have two sidws with C ans S n times using recursion method ?

For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:

CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS

What I have tried:

How do I print all possible ways if I drop card have two sidws with C ans S n times?

For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:

CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS
1
Expert's answer
2016-03-18T15:48:05-0400
public class Q58434 {
public static void main(String[] args){
int number = 4;
String out = allPosibleWaysRecursion(number);
System.out.println(out);
out = allPosibleWays(number);
System.out.println(out);
}

public static String allPosibleWaysRecursion(int number) {
if(number==1) return " C S";
else return allPosibleWaysRecursion(number-1).replaceAll(" ", " C")+
allPosibleWaysRecursion(number-1).replaceAll(" ", " S");
}

public static String allPosibleWays(int number) {
String out = "", tmp;
for(int i = (int) Math.pow(2, number)-1; i>=0; i--){
tmp = Integer.toBinaryString(i);
while(tmp.length()<number){
tmp = "0"+tmp;
}
tmp = tmp.replaceAll("0", "S");
tmp = tmp.replaceAll("1", "C");
out += tmp +" ";
}
return out;
}
}

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
APPROVED BY CLIENTS