有6个数字,"431563",怎样输出所有的排列组合,但要遵守一定的规则,4和5不能相邻,最后一个不能是3

解决方案 »

  1.   

    import   java.util.*; 
    public   class   test   { 
    public   static   void   main(String[]   arg)   { 
    Scanner   r=new   Scanner(System.in); 
    String   s=r.nextLine(); 
    Pailie(s, " "); 

    static   void   Pailie(String   s,   String   p)   { 
    if(s.length() <1)   { 
    String   t=p+s; 
    if(t.charAt(2)!= '4 '   &&   t.contains( "35 ")==false) 
    System.out.println(t); 

    else   { 
    for(int   i=0;   i <s.length();   i++)   { 
    Pailie(s.substring(1),p+s.substring(0,1)); 
    s=s.substring(1)+s.substring(0,1); 



    }
      

  2.   

    import java.util.*;public class AllSort{
    static Set<String> set = new TreeSet<String> ();

        public static void main(String[] args) {
            char buf[]={'1','3','3','4','5','6'};
            int count = 0;        perm(buf,0,buf.length-1);
            System.out.println(set.size());
            
            for(String s : set){     
             System.out.print(s +" ");
            
             count ++;
            
             if (count % 10 == 0)
             System.out.println();
            }
        }
        public static void perm(char[] buf,int start,int end){
            if(start==end){//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
                String output = new String(buf);
                
             if (output.charAt(5) == '3' || output.contains("45"))
                 return;
            
                set.add(output);
            }
            else{//多个字母全排列
                for(int i=start;i<=end;i++){
                    char temp=buf[start];//交换数组第一个元素与后续的元素
                    buf[start]=buf[i];
                    buf[i]=temp;
                    
                    perm(buf,start+1,end);//后续元素递归全排列
                    
                    temp=buf[start];//将交换后的数组还原
                    buf[start]=buf[i];
                    buf[i]=temp;
                }
            }
        }
    }
    204
    133465 133546 133564 133654 134356 134365 134635 135346 135364 135436 
    135634 136354 136435 136534 143356 143365 143536 143635 146335 153346 
    153364 153436 153634 154336 156334 163354 163435 163534 164335 165334 
    313465 313546 313564 313654 314356 314365 314635 315346 315364 315436 
    315634 316354 316435 316534 331465 331546 331564 331654 334156 334165 
    334615 334651 335146 335164 335416 335461 335614 335641 336154 336415 
    336514 336541 341356 341365 341536 341635 343156 343165 343516 343561 
    343615 343651 346135 346315 346351 346531 351346 351364 351436 351634 
    353146 353164 353416 353461 353614 353641 354136 354316 354361 354631 
    356134 356314 356341 356431 361354 361435 361534 363154 363415 363514 
    363541 364135 364315 364351 365134 365314 365341 365431 413356 413365 
    413536 413635 415336 416335 431356 431365 431536 431635 433156 433165 
    433516 433561 433615 433651 435136 435316 435361 435631 436135 436315 
    436351 436531 461335 463135 463315 463351 463531 465331 513346 513364 
    513436 513634 514336 516334 531346 531364 531436 531634 533146 533164 
    533416 533461 533614 533641 534136 534316 534361 534631 536134 536314 
    536341 536431 541336 543136 543316 543361 543631 546331 561334 563134 
    563314 563341 563431 564331 613354 613435 613534 614335 615334 631354 
    631435 631534 633154 633415 633514 633541 634135 634315 634351 635134 
    635314 635341 635431 641335 643135 643315 643351 643531 651334 653134 
    653314 653341 653431 654331 
      

  3.   

    public class TestSort {
    public static int count(String str,char ch) {
    int n = 0;
    int index = 0;
    while((index =str.indexOf(ch,index)) != -1) {
    n++;
    index++;
    }
    return n;
    }
    public static void main(String[] args) {
    String str = "431563";
    String sortString = null;
    char[] arry = str.toCharArray();
    char[] arry1 = new char[6];
    for (int i = 0;i < arry.length;i++) {
    arry1[0] = arry[i];
    for (int j = 0;j < arry.length;j++) {
    arry1[1] = arry[j];
    for(int k = 0;k < arry.length;k++) {
    arry1[2] = arry[k];
    for(int m = 0; m < arry.length;m++) {
    arry1[3] = arry[m];
    for(int n = 0; n < arry.length;n++) {
    arry1[4] = arry[n];
    for(int g = 0;g < arry.length;g++) {
    arry1[5] = arry[g];
    sortString = new String(arry1);
    if (sortString.indexOf("45") != -1 || arry1[5] == '3' || count(sortString,'4') != 1 ||count(sortString,'5') != 1 ||count(sortString,'6') != 1 || count(sortString,'1') != 1 || count(sortString,'3') != 2) {
    continue;
    } else {
    System.out.println(sortString);
    }
    }
    }
    }
    }
    }
    }
    }
    }
      

  4.   

    更正一下public class TestSort {
        public static int count(String str,char ch) {
            int n = 0;
            int index = 0;
            while((index =str.indexOf(ch,index)) != -1) {
                n++;
                index++;
            }
            return n;
        }
        public static void main(String[] args) {
            String str = "431563";
            String sortString = null;
            int count = 0;
            char[] arry = str.toCharArray();
            char[] arry1 = new char[6];
            for (int i = 0;i < arry.length;i++) {
                arry1[0] = arry[i];
                for (int j = 0;j < arry.length;j++) {
                    arry1[1] = arry[j];
                    for(int k = 0;k < arry.length;k++) {
                        arry1[2] = arry[k];
                        for(int m = 0; m < arry.length;m++) {
                            arry1[3] = arry[m];
                            for(int n = 0; n < arry.length;n++) {
                                arry1[4] = arry[n];
                                for(int g = 0;g < arry.length;g++) {
                                    arry1[5] = arry[g];
                                    sortString = new String(arry1);
                                    if (sortString.indexOf("54") != -1 || sortString.indexOf("45") != -1 || arry1[5] == '3' || count(sortString,'4') != 1 ||count(sortString,'5') != 1 ||count(sortString,'6') != 1 || count(sortString,'1') != 1 || count(sortString,'3') != 2) {//少了判断"54"了
                                        continue;
                                    } else {
                                    
                                     if(count % 10 == 0)
                                     System.out.println();
                                     count++;
                                        System.out.print(" "+sortString);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }433156 433165 433516 433561 433615 433651 431356 431365 431536 431536
     431635 431635 431356 431365 435316 435361 435136 435136 435631 435631
     435316 435361 436315 436351 436135 436135 436531 436531 436315 436351
     433156 433165 433516 433561 433615 433651 413356 413365 413536 413536
     413635 413635 413356 413365 415336 415336 415336 415336 416335 416335
     416335 416335 413356 413365 413536 413536 413635 413635 413356 413365
     463315 463351 463135 463135 463531 463531 463315 463351 461335 461335
     461335 461335 465331 465331 465331 465331 463315 463351 463135 463135
     463531 463531 463315 463351 433156 433165 433516 433561 433615 433651
     431356 431365 431536 431536 431635 431635 431356 431365 435316 435361
     435136 435136 435631 435631 435316 435361 436315 436351 436135 436135
     436531 436531 436315 436351 433156 433165 433516 433561 433615 433651
     343156 343165 343516 343561 343615 343651 341356 341365 341536 341536
     341635 341635 341356 341365 346315 346351 346135 346135 346531 346531
     346315 346351 343156 343165 343516 343561 343615 343651 334156 334165
     334615 334651 331465 331564 335146 335164 335641 335614 336415 336514
     314356 314365 314635 314635 314356 314365 313465 313564 315346 315364
     315634 315634 315346 315364 316435 316435 316534 316534 313465 313564
     353416 353461 353146 353164 353641 353614 351436 351436 351346 351364
     351634 351634 351346 351364 356431 356431 356341 356314 356134 356134
     356341 356314 353416 353461 353146 353164 353641 353614 364315 364351
     364135 364135 364315 364351 363415 363514 361435 361435 361534 361534
     365341 365314 365134 365134 365341 365314 363415 363514 334156 334165
     334615 334651 331465 331564 335146 335164 335641 335614 336415 336514
     143356 143365 143536 143536 143635 143635 143356 143365 146335 146335
     146335 146335 143356 143365 143536 143536 143635 143635 143356 143365
     134356 134365 134635 134635 134356 134365 133465 133564 135346 135364
     135634 135634 135346 135364 136435 136435 136534 136534 133465 133564
     153436 153436 153346 153364 153634 153634 153346 153364 156334 156334
     156334 156334 153436 153436 153346 153364 153634 153634 153346 153364
     164335 164335 164335 164335 163435 163435 163534 163534 165334 165334
     165334 165334 163435 163435 163534 163534 134356 134365 134635 134635
     134356 134365 133465 133564 135346 135364 135634 135634 135346 135364
     136435 136435 136534 136534 133465 133564 534316 534361 534136 534136
     534631 534631 534316 534361 533416 533461 533146 533164 533641 533614
     531436 531436 531346 531364 531634 531634 531346 531364 536431 536431
     536341 536314 536134 536134 536341 536314 533416 533461 533146 533164
     533641 533614 514336 514336 514336 514336 513436 513436 513346 513364
     513634 513634 513346 513364 516334 516334 516334 516334 513436 513436
     513346 513364 513634 513634 513346 513364 564331 564331 564331 564331
     563431 563431 563341 563314 563134 563134 563341 563314 561334 561334
     561334 561334 563431 563431 563341 563314 563134 563134 563341 563314
     534316 534361 534136 534136 534631 534631 534316 534361 533416 533461
     533146 533164 533641 533614 531436 531436 531346 531364 531634 531634
     531346 531364 536431 536431 536341 536314 536134 536134 536341 536314
     533416 533461 533146 533164 533641 533614 643315 643351 643135 643135
     643531 643531 643315 643351 641335 641335 641335 641335 643315 643351
     643135 643135 643531 643531 643315 643351 634315 634351 634135 634135
     634315 634351 633415 633514 631435 631435 631534 631534 635341 635314
     635134 635134 635341 635314 633415 633514 614335 614335 614335 614335
     613435 613435 613534 613534 615334 615334 615334 615334 613435 613435
     613534 613534 653431 653431 653341 653314 653134 653134 653341 653314
     651334 651334 651334 651334 653431 653431 653341 653314 653134 653134
     653341 653314 634315 634351 634135 634135 634315 634351 633415 633514
     631435 631435 631534 631534 635341 635314 635134 635134 635341 635314
     633415 633514 343156 343165 343516 343561 343615 343651 341356 341365
     341536 341536 341635 341635 341356 341365 346315 346351 346135 346135
     346531 346531 346315 346351 343156 343165 343516 343561 343615 343651
     334156 334165 334615 334651 331465 331564 335146 335164 335641 335614
     336415 336514 314356 314365 314635 314635 314356 314365 313465 313564
     315346 315364 315634 315634 315346 315364 316435 316435 316534 316534
     313465 313564 353416 353461 353146 353164 353641 353614 351436 351436
     351346 351364 351634 351634 351346 351364 356431 356431 356341 356314
     356134 356134 356341 356314 353416 353461 353146 353164 353641 353614
     364315 364351 364135 364135 364315 364351 363415 363514 361435 361435
     361534 361534 365341 365314 365134 365134 365341 365314 363415 363514
     334156 334165 334615 334651 331465 331564 335146 335164 335641 335614
     336415 336514
      

  5.   

    import java.util.*;public class AllSort{
        static Set<String> set = new TreeSet<String> ();
        
        public static void main(String[] args) {
            char buf[]={'1','3','3','4','5','6'};
            int count = 0;        perm(buf,0,buf.length-1);
            System.out.println(set.size());
            
            for(String s : set){         
                System.out.print(s +" ");
                
                count ++;
                
                if (count % 10 == 0)
                    System.out.println();
            }
        }
        public static void perm(char[] buf,int start,int end){
            if(start==end){//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
                String output = new String(buf);
                
                if (output.charAt(5) == '3' || output.contains("45")|| output.contains("54"))
                    return;
                
                set.add(output);
            }
            else{//多个字母全排列
                for(int i=start;i<=end;i++){
                    char temp=buf[start];//交换数组第一个元素与后续的元素
                    buf[start]=buf[i];
                    buf[i]=temp;
                    
                    perm(buf,start+1,end);//后续元素递归全排列
                    
                    temp=buf[start];//将交换后的数组还原
                    buf[start]=buf[i];
                    buf[i]=temp;
                }
            }
        }
    }
    8楼的还漏了一种情况,红色部分.
    一共有168个!
      

  6.   

    if (output.charAt(5) == '3' || output.contains("45")|| output.contains("54"))