首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

大家伙儿来帮一下忙啊

2011-12-03 
大家来帮一下忙啊编写带有下列声明的例程publicvoidpermute(Stringstr)privatevoidpermute(char[]str,int

大家来帮一下忙啊
编写带有下列声明的例程
public   void   permute(String   str);
private   void   permute(char   []   str,int   low,int   high);

第一个例程是一个驱动程序,它调用第二个例程并显示String   str中字符的所有排列。如果str是

“abc”,那么输出的串是abc,acb,bac,bca,cab,cba.第二个例程使用递归。


[解决办法]
记得结帖~
public class TestPermute {
static long count = 0;
public static void main(String[] args) {
TestPermute tp = new TestPermute();
tp.permute( "abcdefg ");
System.out.println(count);
}

public void permute(String str) {
permute(str.toCharArray(), 0, str.length()-1);
}

private void permute(char[] str, int low, int high) {

for (int i = low; i <= high; i++) {
swap(str, low, i);
if (low + 1 == high) {
System.out.println(new String(str));
count++;
} else
permute(str, low + 1, high);
swap(str, i, low);
}
}

private void swap(char[] str, int low, int high) {
char temp = str[low];
str[low] = str[high];
str[high] = temp;
}
}

热点排行