给你 几个 字母 求它的全排列 ~~! 给你 几个 字母 求它的全排列 ~~!比如说 abcd 求他们的全排列! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class PaiLie { public static void main(String args[]) { int leng = args[0].length(); int[] a = new int[leng]; boolean[] c = new boolean[leng]; for (int i = 0; i < leng; i++) { a[i] = i + 1; c[i] = false; } int ii = findActiveM(a, c); for (int i = 0; i < args[0].length(); i++) System.out.print(a[i]); System.out.println( ) ; if (ii >= 0) { do { int activeM= a[ii] ; if (c[ii] == false) { swapInt(a,ii, ii - 1); swapBoolean(c,ii, ii - 1); } else { swapInt(a,ii, ii + 1); swapBoolean(c,ii, ii + 1); } for (int i = 0; i < args[0].length(); i++) { System.out.print(a[i]); if (a[i] > activeM) c[i] = !c[i]; } System.out.println(); ii = findActiveM(a, c); } while (ii >= 0); } } private static void swapInt(int[] aa , int a, int b) { int temp = aa[a]; aa[a] = aa[b]; aa[b] = temp; } private static void swapBoolean(boolean[] aa , int a , int b) { boolean temp = aa[a]; aa[a] = aa[b]; aa[b] = temp; } private static int findActiveM(int[] a, boolean[] c) { int first1 = 0, first2 = 0; int m = -1, n = -1; for (int i = 1; i < a.length - 1; i++) { if (c[i] == false) { if ((a[i] > a[i - 1]) && a[i] > first1) { first1 = a[i]; m = i; } } else { if ((a[i] > a[i + 1]) && a[i] > first2) { first2 = a[i]; n = i; } } } if ((c[0] == true) && (a[0] > a[1]) && (a[0] > first2)) { first2 = a[0]; n = 0; } if ((c[a.length - 1] == false) && (a.length>1) && (a[a.length - 1] > a[a.length - 2]) && (a[a.length - 1] > first1)) { first1 = a[a.length - 1]; m = a.length - 1; } if (first1 >= first2) return m; else return n; }} 看看这个keyibu ? 是不是就是abcd acbd acdb... 哥们 别去 copy 阿 ~~ 你给那一大段 看不懂 没用比如说 给abc 要求输出如下:abc acb bac bca cab abc acb abc bca bac cba cab 这个可行 但是我 看不懂 哪位 高手 解释一下行吗???请看public class QuanPaiLei { public static int MAX = 3; public static boolean state[] = new boolean[MAX + 1]; public static int item[] = new int[MAX + 1]; public static String element[] = { "a", "b", "c" }; public static void main(String[] args) { DoPermutation(1); } public static void DoPermutation(int pos) { if (pos > MAX) { for (int j = 1; j <= MAX; j++) System.out.print(element[item[j] - 1]); System.out.println(); return; } for (int i = 1; i <= MAX; i++) { if (!state[i]) { state[i] = true; item[pos] = i; DoPermutation(pos + 1); state[i] = false; } } }} 我用debug 一步一步观察都 看晕了 import java.util.HashSet;public class Queque { /** * 对字符串queStr按照字母进行全排列 * * @param queStr String 需要排列的字符串 * @param outStr String 已经出列的字符串(初始时为"") * @return queuesRs HashSet 其元素为String,保存全排列后的结果 */ public HashSet makeQueues(String outStr, String queStr) { HashSet queuesRs = new HashSet(); if(queStr.length()==1) { queuesRs.add(outStr+queStr); queuesRs.add(queStr+outStr); } else if(queStr.length()==2) { queuesRs.add(outStr+queStr.charAt(0)+queStr.charAt(1)); queuesRs.add(outStr+queStr.charAt(1)+queStr.charAt(0)); } else { for(int i=0;i<queStr.length();i++) { //将要排列的字符串的每个字符分别出列,出列字符追加到原已出列字符串中,同时对新出列字符串和剩余需全排列字符串进行递归 queuesRs.addAll(makeQueues(outStr+queStr.charAt(i),this.remove(queStr,i))); } } return queuesRs; } /* * 删除字符串中指定索引处的字符,返回删除后的新字符串 */ String remove(String str,int idx) { return str.substring(0,idx)+str.substring(idx+1); } public static void main(String[]args) { Queque testQue = new Queque(); String testStr = "abc"; HashSet queuesRs = testQue.makeQueues("", testStr); System.out.println("全排列结果:"+queuesRs); System.out.println("全排列打印数量:"+queuesRs.size()); int num=1; for(int i=1;i<=testStr.length();i++) { num*=i; } System.out.println("实际全排列数量:"+num); }}-------------------------------------------------------------------------------------全排列结果:[bca, acb, bac, cab, cba, abc]全排列打印数量:6实际全排列数量:6 问一道数学题 struts2---如何使myeclipse和tomcat同步呢? eclipse 快捷键。 消费者、生产者问题之问题 java.lang.ClassNotFoundException: oracle.jdbc.driver.oracledriver的问题 求一算法 怎么把java 程序做成后台服务程序? 請問調用接口方法的速度會比調用對象方法的速度慢嗎? lookAndfeel的jar包怎么用? 关于字数统计的小问题,我的程序只能查奇数行 java写的两个椭圆相互靠拢的动态演示,有点问题 父类窗体有3个按钮,但个个子类继承过来后每个button的相应不同,请问如何做?
public class PaiLie {
public static void main(String args[]) {
int leng = args[0].length();
int[] a = new int[leng];
boolean[] c = new boolean[leng];
for (int i = 0; i < leng; i++) {
a[i] = i + 1;
c[i] = false;
}
int ii = findActiveM(a, c); for (int i = 0; i < args[0].length(); i++)
System.out.print(a[i]);
System.out.println( ) ; if (ii >= 0) {
do {
int activeM= a[ii] ;
if (c[ii] == false) {
swapInt(a,ii, ii - 1);
swapBoolean(c,ii, ii - 1);
} else {
swapInt(a,ii, ii + 1);
swapBoolean(c,ii, ii + 1);
}
for (int i = 0; i < args[0].length(); i++) {
System.out.print(a[i]);
if (a[i] > activeM)
c[i] = !c[i];
}
System.out.println();
ii = findActiveM(a, c);
} while (ii >= 0);
}
}
private static void swapInt(int[] aa , int a, int b) {
int temp = aa[a];
aa[a] = aa[b];
aa[b] = temp;
} private static void swapBoolean(boolean[] aa , int a , int b) {
boolean temp = aa[a];
aa[a] = aa[b];
aa[b] = temp;
}
private static int findActiveM(int[] a, boolean[] c) { int first1 = 0, first2 = 0;
int m = -1, n = -1;
for (int i = 1; i < a.length - 1; i++) {
if (c[i] == false) {
if ((a[i] > a[i - 1]) && a[i] > first1) {
first1 = a[i];
m = i;
}
} else {
if ((a[i] > a[i + 1]) && a[i] > first2) {
first2 = a[i];
n = i;
}
}
}
if ((c[0] == true) && (a[0] > a[1]) && (a[0] > first2)) {
first2 = a[0];
n = 0;
}
if ((c[a.length - 1] == false) && (a.length>1)
&& (a[a.length - 1] > a[a.length - 2])
&& (a[a.length - 1] > first1)) {
first1 = a[a.length - 1];
m = a.length - 1;
}
if (first1 >= first2)
return m;
else
return n;
}
}
看看这个keyibu ?
要求输出如下:
abc acb bac bca cab abc
哪位 高手 解释一下行吗???
请看public class QuanPaiLei { public static int MAX = 3; public static boolean state[] = new boolean[MAX + 1]; public static int item[] = new int[MAX + 1]; public static String element[] = { "a", "b", "c" }; public static void main(String[] args) {
DoPermutation(1);
} public static void DoPermutation(int pos) {
if (pos > MAX) {
for (int j = 1; j <= MAX; j++)
System.out.print(element[item[j] - 1]);
System.out.println();
return;
}
for (int i = 1; i <= MAX; i++) {
if (!state[i]) {
state[i] = true;
item[pos] = i;
DoPermutation(pos + 1);
state[i] = false;
}
}
}
}
import java.util.HashSet;public class Queque { /**
* 对字符串queStr按照字母进行全排列
*
* @param queStr String 需要排列的字符串
* @param outStr String 已经出列的字符串(初始时为"")
* @return queuesRs HashSet 其元素为String,保存全排列后的结果
*/
public HashSet makeQueues(String outStr, String queStr)
{
HashSet queuesRs = new HashSet();
if(queStr.length()==1)
{
queuesRs.add(outStr+queStr);
queuesRs.add(queStr+outStr);
}
else if(queStr.length()==2)
{
queuesRs.add(outStr+queStr.charAt(0)+queStr.charAt(1));
queuesRs.add(outStr+queStr.charAt(1)+queStr.charAt(0));
}
else
{
for(int i=0;i<queStr.length();i++)
{
//将要排列的字符串的每个字符分别出列,出列字符追加到原已出列字符串中,同时对新出列字符串和剩余需全排列字符串进行递归
queuesRs.addAll(makeQueues(outStr+queStr.charAt(i),this.remove(queStr,i)));
}
}
return queuesRs;
}
/*
* 删除字符串中指定索引处的字符,返回删除后的新字符串
*/
String remove(String str,int idx)
{
return str.substring(0,idx)+str.substring(idx+1);
}
public static void main(String[]args)
{
Queque testQue = new Queque();
String testStr = "abc";
HashSet queuesRs = testQue.makeQueues("", testStr);
System.out.println("全排列结果:"+queuesRs);
System.out.println("全排列打印数量:"+queuesRs.size());
int num=1;
for(int i=1;i<=testStr.length();i++)
{
num*=i;
}
System.out.println("实际全排列数量:"+num);
}}
-------------------------------------------------------------------------------------
全排列结果:[bca, acb, bac, cab, cba, abc]
全排列打印数量:6
实际全排列数量:6