当前位置: 代码迷 >> Java相关 >> 排列组合
  详细解决方案

排列组合

热度:573   发布时间:2006-05-23 20:22:00.0
排列组合

楼主帮帮我,我头都想大了,还是写不出来。恕我是初学者,还请体谅///

Problem: Use your most hands-on programming language to implement a function that prints all possible combinations of the characters in a string. These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of the characters ARE the same combination. In other words, “12” and “31” are different combinations from the input string “123”, but “21 is the same as “12”.

For example, if we use “wxyz” as parameter for Combination(“wxyz”) the function will print out


w

x

y

z

wx

wy

wz

xy

xz

yz

wxy

wxz

wyz

xyz

wxyz

搜索更多相关的解决方案: 排列  

----------------解决方案--------------------------------------------------------
刚注册帐号,看到帖子,按照你的要求做了一个.
[CODE]public class pailie{
public static void main(String[] args){
String z=new String("wxyz");
int len=z.length();
String result="";
for(int b=1;b<=len;b++)
pailie(z,result,b);
}
public static void pailie(String z,String result,int b){
String res=result;
if(b==0){
System.out.println(result);
return;
}
int len=z.length();
for(int a=len-1;a>=0;a--){
res=res+z.charAt(a);
pailie(z.substring(0,a),res,b-1);
res=result;
}

}
}[/CODE]
----------------解决方案--------------------------------------------------------

晕啊 为什么我只能看到你贴的一部分


----------------解决方案--------------------------------------------------------
public class pailie{
public static void main(String[] args){
String z=new String("wxyz");
int len=z.length();
String result="";
for(int b=1;b<=len;b++)
pailie(z,result,b);
}
public static void pailie(String z,String result,int b){
String res=result;
if(b==0){
System.out.println(result);
return;
}
int len=z.length();
for(int a=len-1;a>=0;a--){
res=res+z.charAt(a);
pailie(z.substring(0,a),res,b-1);
res=result;
}

}
}
----------------解决方案--------------------------------------------------------
能讲一下你的算法过程吗`?
最好加上注释谢谢`
----------------解决方案--------------------------------------------------------