A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

瞅瞅而已。。。。。。
回复 使用道具 举报
as604049322 发表于 2015-6-1 13:29
combinations("abcd",2);第2个参数,想取几个就取几个

不错,但这个程序考虑了顺序的变化,而且如果字符串较大的话,取的数也大一点,会有点问题,例如:combinations("123456789zhong",9) 。这题目要求不需要考虑顺序变化的情况,比喻12和21就属于同一种情况
回复 使用道具 举报
半月 发表于 2015-6-1 14:05
public class Test2 {
        public static void main(String[] args) {
                String str = "abcde";

这个程序可以,用递归做的,结果和题目要求一样,学习了,感谢
回复 使用道具 举报
半月 发表于 2015-6-1 14:05
public class Test2 {
        public static void main(String[] args) {
                String str = "abcde";

这个程序可以,用递归做的,结果和题目要求一样,学习了,感谢
回复 使用道具 举报
怎么回复重复了,其实今天上午我自己又做了一下,用了两种方法做的,第一种和楼上的一样都是用递归做的,第二种用的是数组加循环做的。总算自己也做出来了,非常感谢大家的参与和帮忙
回复 使用道具 举报
分享一下上午做出来的两种方法:(时间太快,没写注释)
1.用递归做的:public class StringSort3 {
        public static int count=0;
        public static void main(String[] args){
                String str="123456789";
                sort("",str,7);
                System.out.println(count);
        }
        public static void sort(String strPre,String str,int value){
                if(value==0)
                        return;
                String temp="";
                String strL="";
                for(int x=0;x<=str.length()-value;x++){
                        temp=strPre;
                        strPre=strPre+str.substring(x,x+1);
                        if(value==1){
                                System.out.println(strPre);
                                count++;
                        }
                        strL=str.substring(x+1,str.length());
                        value-=1;
                        sort(strPre,strL,value);
                        value+=1;
                        strPre=temp;
                }
        }
}

第二种方法:数组加循环
  1. public class StringSort2 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static int count=0;
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 String str="0123456789";
  9.                 sort(str,4);
  10.                 System.out.println(count);
  11.         }
  12.        
  13.         public static void sort(String str,int value){
  14.                 int pos1=0,pos2=0;
  15.                 char[] ch= str.toCharArray();
  16.                 char[] temp=new char[value];
  17.                 for(int x=0;x<temp.length;x++)
  18.                         temp[x]=ch[x];
  19.                 System.out.println(String.valueOf(temp));
  20.                 count++;
  21.                 for(int i=value;i<=ch.length;i++){
  22.                         if(i<ch.length){
  23.                                 temp[value-1]=ch[i];
  24.                                 System.out.println(String.valueOf(temp));
  25.                                 count++;
  26.                         }
  27.                         else
  28.                                 i=ch.length-1;
  29.                         if(i==ch.length-1){
  30.                                 for(int a=1;a<=value;a++){
  31.                                         if(temp[value-a]!=ch[ch.length-a]){
  32.                                                 pos1=a;
  33.                                                 break;
  34.                                         }
  35.                                 }
  36.                                 for(int b=0;b<ch.length;b++){
  37.                                         if(temp[value-pos1]==ch[b]){
  38.                                                 pos2=b;
  39.                                                 break;
  40.                                         }
  41.                                 }
  42.                                 for(int a=pos1,b=pos2;a>0;a--,b++){
  43.                                         temp[value-a]=ch[b+1];
  44.                                 }
  45.                                 System.out.println(String.valueOf(temp));
  46.                                 count++;
  47.                                 if(temp[0]==ch[ch.length-value])
  48.                                         return;
  49.                                 i=pos1+pos2;
  50.                         }
  51.                 }
  52.         }
复制代码

回复 使用道具 举报
本帖最后由 as604049322 于 2015-6-1 16:33 编辑
探索者 发表于 2015-6-1 15:56
不错,但这个程序考虑了顺序的变化,而且如果字符串较大的话,取的数也大一点,会有点问题,例如:combin ...

早知道是不用考虑顺序的我就不用这么蛋疼了。。。
  1. package cn.wangzhe;

  2. import java.util.LinkedList;


  3. /*
  4. 在求一个字符串中所有字符的组合的时候,针对一个字符,有两种情况,假设在长度为n的字符串中选择长度为m的组合字符串,

  5. 第一是选择长度为n的字符串中的第一个字符,那么要在其余的长度n-1的字符串中选择m-1个字符

  6. 第二是不选择长度为n的字符串中的第一个字符,那么要在其余的长度n-1的字符串中选择m个字符

  7. 递归结束的条件就是,当m为0,即从字符串中不再选出字符的时候,这个时候已经找到了m个字符的组合,输出即可。
  8. 还有一个条件是,当输入的字符串是空,自然是不能从中选出任何字符的。
  9. */
  10. public class CombinationStr{
  11.         public static void main(String[] args) {
  12. //                combination("abcd");
  13.                 combination("abcde",3);
  14.         }

  15.     public static void combination(String str,int len){
  16.             combination("abcde",len,new StringBuilder());
  17.     }
  18.     private static void combination(String str,int m,StringBuilder result){//c(n,m)
  19.         if(m==0){
  20.                 System.out.print(result+" ");
  21.             return ;
  22.         }
  23.         if(str.length()>0){
  24.             result.append(str.charAt(0));
  25.             combination(str.substring(1),m-1,result);
  26.             result.deleteCharAt(result.length()-1);
  27.             combination(str.substring(1),m,result);
  28.         }
  29.     }


  30. }
复制代码


回复 使用道具 举报
难度系数大,思考思考!!!
回复 使用道具 举报
as604049322 发表于 2015-6-1 16:30
早知道是不用考虑顺序的我就不用这么蛋疼了。。。

感觉考虑顺序比较麻烦,不过做出了不用考虑顺序的程序,再做个可以进行全排的,结合起来就和你那个的效果差不多了
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马