여름 동안 프로그래밍 과정을 공부했습니다.
상당히 표준적인 과제이며 많은 게시물과 비디오를 보았지만 제대로 작동하지 않습니다!
"문자열의 모든 순열을 인쇄하는 재귀 적 방법을 완료하십시오."
public static void permute(String s, PrintStream out) {
permute(s, 0, out);
}
private static void permute(String s, int pos, PrintStream out) {
// Fill in your own code
}
나는 몇 가지 해결책을 보았지만, 주어진 형식으로 내 손은 다소 묶여있다. 코드를 컴파일하는 데 사용하는 플랫폼을 얻기 위해 조금만 놀았습니다. 내가 생각해 낸 것은 예쁘지 않고 정말 붙어 있습니다!
public static void permute(String s, PrintStream out) {
permute(s, 0, out);
}
private static void permute(String s, int pos, PrintStream out) {
out.println(s);
for(int i = pos; i<s.length()-1; i++) {
StringBuilder sb = new StringBuilder();
sb.append(s);
if(i== pos+1) {
sb.setCharAt(0, s.charAt(s.length()-1));
sb.setCharAt(2, s.charAt(s.length()-s.length()));
permute(sb.toString(),i,out);
}else {
sb.setCharAt(1, s.charAt(s.length()-1));
sb.setCharAt(2, s.charAt(s.length()-2));
out.println(sb.toString());
}
}
}
ABC ACB CAB CBA를 인쇄합니다. 이 코드에 많은 문제가 있음을 알고 있습니다. 나는 ABC를 다루기 위해 구체적으로 썼고 거기서부터 알아낼 것입니다. 나는 짧게 짧게 나왔다. 모든 통찰력, 팁 또는 피드백을 환영합니다! 감사합니다!
다음과 같은 방법으로 String을 재귀 순열시킬 수 있습니다.
static int count=0;
public static void main(String args[]) {
permutation("", "JASPER");
// Gives the total permutated word count
System.out.println(count);
}
private static void permutation(String perm, String word) {
if (word.isEmpty()) {
System.err.println(perm + word);
} else {
for (int i = 0; i< word.length(); i++) {
permutation(perm + word.charAt(i), word.substring(0, i)
+ word.substring(i + 1, word.length()));
count++;
}
}
}
이것은 주어진 문자열에서 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다