1,2,3,4,5,6六个数字,4不能在第三位,3,5不能相邻,打印所有排列。
给个思想就好了,我自己动手编,实在是没有分。谢谢各位了。

解决方案 »

  1.   

    第一种办法:
    排列的总结果是:
    123456---654321
    if(数字第四位不等于3 然后 可以采用模式匹配算法 匹配45 或者 54 是否存在这个组中)
    用JAVA 就很好实现了
    第二种办法:( 主要考虑实用性 处理字符等...)
    就一布一布的实现
    public void deal(int [] number){
       int len = number.length;//求出 长度
       if(len==6){...执行打印...}
       else{
          执行 添加一个 数据 然后 继续递归
           产生一个1-6之间的数字
           判断这个数字将要插入位置是M 是否等于4 如果 此时产生的数字是3 那么则重新产生数字
           如果不是 就判断 是否 出现45相邻的情况
           如果 不存在 就添加 然后继续递归       下面 再继续产生其他情况 继续递归
       }
    }
      

  2.   

    我给你用第二种方式 写的代码:
    第一种调用库函数 就 简单了public class T {
      public static void main(String[] args){
      int test[] = {1,2,3,4,5,6};
      int []rn = new int[1];
      for(int i=1;i<=6;i++){
      rn[0]=i;
      range(rn,test);
      }
      
      }
      public static void range(int [] number,int [] object){
      int len = number.length;  
      if(len==object.length){
      for(int i=0;i<object.length;i++){
      System.out.print(number[i]+" ");
      }
      System.out.println();   
       }else{
      for(int n=0;n<object.length;n++){
      int num = object[n];//num是 随机值
      //判断是否出现在 已知的序列中
      boolean exist=false;
      for(int j=0;j<number.length;j++){
      if(number[j]==num){
      exist = true;
      break;
      }
      }
      if(exist) continue;
    //判断 是否是在第三个位置
      if(num==4){
      if(number.length==2) continue; //继续下一次循环
      }
      //判断 三和五是否在一起
      if(num==3){
      if(number[number.length-1]==5) continue; //继续下一次循环
      }
      if(num==5){
      if(number[number.length-1]==3) continue; //继续下一次循环
      }
      //进行 插入序列的操作
      int []copy=new int[number.length+1];
      for(int i=0;i<number.length;i++) copy[i]=number[i];//实现拷贝
      copy[copy.length-1]=num;
      range(copy,object);
      }
      }
       }
    }
      

  3.   

    我刚刚 说错了 第一种说法是错的 是判断连续的 下面的方法  
    public class T2 {
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int a[]={3,5};
            int b[]={5,3};
    for(int i=123456;i<=124564;i++){
            //判断第三位是不是四
            int s0=i/10000;
            int num=i-s0*10000;
            int k =num/1000;//取出了第三位的数字
               if(k==4) continue;
            //判断是否存在3和5相邻的
            //采用模式匹配算法
            int object[]= new int[6];
            int s=1;
            while(s<=6){
            object[s-1]=(i-(i/(int)Math.pow(10, (6-s+1)))*(int)Math.pow(10, (6-s+1)))/(int)Math.pow(10, (6-s));
            s++;          
            }
           if(match(a,object)==-1 && match(b,object)==-1){
           System.out.println(i);
           }
            
          }


    }
    public static  int match(int []sub,int[]object){
    //这里我们只 匹配一次 
    int pos=-1;//匹配的位置
    int index=0;//字串的下标
    for(int m=0;m<object.length;m++){ //如果 查找都没有匹配则说明没有 模式匹配失败
    if(object[m]==sub[index]){
    //如果相等 就说明 在当前的字符是匹配的
    if(pos==-1){
    pos=m;
    }
    index++;
    }else{
    //如果 不匹配
    if(pos!=-1) {
    m=pos;
    pos=-1;
    }
    index=0;
    }
    if(index==sub.length){
    return pos;
    }
    }
    return pos;
    }}
      

  4.   

    public class Test
    { /**
     * @param args
     */
    public static void main(String[] args)
    {

    int[] arr = { 0, 1, 2, 3, 4, 5, 6 };
    perm(arr, 1); } public static void perm(int[] num, int i)
    {
    if (i < num.length - 1)
    {
    for (int j = i; j <= num.length - 1; j++)
    {
    int tmp = num[j];  
    for (int k = j; k > i; k--)
    num[k] = num[k - 1];
    num[i] = tmp;
    perm(num, i + 1);   
    for (int k = i; k < j; k++)
    num[k] = num[k + 1];
    num[j] = tmp;
    }
    } else
    {   
    for (int j = 1; j <= num.length - 1; j++)
    {
    if (num[4] == 3 || (num[j - 1] == 3 && num[j] == 5)
    || (num[j - 1] == 5 && num[j] == 3))
    continue;
    System.out.print(num[j] + " ");
    }
    System.out.println();
    }
    }
    }
      

  5.   

    刚才那个有点问题!
    public class Test
    { /**
     * @param args
     */
    public static void main(String[] args)
    {

    int[] arr = { 0, 1, 2, 3, 4, 5, 6 };
    perm(arr, 1); } public static void perm(int[] num, int i)
    {
    if (i < num.length - 1)
    {
    for (int j = i; j <= num.length - 1; j++)
    {
    int tmp = num[j];    
    for (int k = j; k > i; k--)
    num[k] = num[k - 1];
    num[i] = tmp;
    perm(num, i + 1);   
    for (int k = i; k < j; k++)
    num[k] = num[k + 1];
    num[j] = tmp;
    }
    } else
    {  
    for (int j = 1; j <= num.length - 1; j++)
    {
    if (num[4] == 4 || (num[j - 1]* num[j] ==15)){};

    System.out.print(num[j] + " ");
    }
    System.out.println();
    }
    }
    }
      

  6.   

    public class Permutation {   public static void main(String args[])
       {
        int a[][] = new int[6][1];
            for ( int i = 0; i < 6; i++)
            {
             a[i] = new int[1];
             a[i][0] = i+1;
            }
        perm(a,0);
       }
       public static void perm(int a[][],int k )
       {
           
           if (k == a.length-1 && a[2][0] != 4 )
           {      
                  int m = 0, n = 0, l = 0;
                  for ( int i = 0; i < a.length; i++)
                  {
                   if ( a[i][0] == 3)
                      m = i;
                   if ( a[i][0] == 5)
                      n = i;
                  }
                  l = Math.abs(m-n);
                  if (l != 2)
                  {
                  
                  
                  for (int i = 0; i < a.length; i++)
          {
           System.out.printf("%d   ",a[i][0]);
          }
          System.out.println("");
                  }
           }
           else 
           {
           
           for ( int i = k; i < 6; i++)
           {
               swap(a[i], a[k]);
               perm(a,k+1);
               swap(a[i],a[k]);
               
           }
           }
       } 
      
       public static  void swap(int a[], int b[])
       {
           int temp = a[0];
           a[0] = b[0];
           b[0] = temp;
       }
        
    }
      

  7.   

    public class Permutation {   public static void main(String args[])
       {
        int a[][] = new int[6][1];
            for ( int i = 0; i < 6; i++)
            {
             a[i] = new int[1];
             a[i][0] = i+1;
            }
        perm(a,0);
       }
       public static void perm(int a[][],int k )
       {
           
           if (k == a.length-1 && a[2][0] != 4 )
           {      
                  int m = 0, n = 0, l = 0;
                  for ( int i = 0; i < a.length; i++)
                  {
                   if ( a[i][0] == 3)
                      m = i;
                   if ( a[i][0] == 5)
                      n = i;
                  }
                  l = Math.abs(m-n);
                  if (l != 1)
                  {
                  
                  
                  for (int i = 0; i < a.length; i++)
          {
           System.out.printf("%d   ",a[i][0]);
          }
          System.out.println("");
                  }
           }
           else 
           {
           
           for ( int i = k; i < 6; i++)
           {
               swap(a[i], a[k]);
               perm(a,k+1);
               swap(a[i],a[k]);
               
           }
           }
       } 
      
       public static  void swap(int a[], int b[])
       {
           int temp = a[0];
           a[0] = b[0];
           b[0] = temp;
       }
        
    }
      

  8.   

     在排列算法的基础上输出的时候加一些条件限制,如此而已;
    package com.test;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    int[] a = { 1, 2, 3, 4,5,6 };
    test(a, 0); } private static void test(int[] a, int index) {
    if (index == a.length) {
    if(a[2]==4)return;
    else{
    for (int i = 0; i < a.length; i++) {
     if(i!=a.length-1){
     if((a[i]==5&&a[i+1]==3)||(a[i]==3&&a[i+1]==5))
     return;
     
     }
    }

    for (int i = 0; i < a.length; i++) {
    System.out.print(a[i] + " ");
    }
    System.out.println();
    return;
    }

    }
    for (int i = index; i < a.length; i++) { swap(a, index, i);
    test(a, index + 1);
    swap(a, i, index);
    } } private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp; }}
      

  9.   

    老题了,懒得写,LZ看看老帖吧!http://topic.csdn.net/u/20080515/17/a3bba247-4970-45c2-bd94-1478a39ef4bd.html
      

  10.   

    我这有一个笨算法
    import java.util.Arrays;public class Demo {
    public static void main(String[] args) {
    int count = 0;
    for(int i=123456; i<=654321; i++) {
    if(check6(i) && !check4(i) && !check35(i) && !checkSameNum(i)) {
    System.out.print(i + "  ");
    count ++;
    if(count%10 == 0)
    System.out.println();
    }
    }
    System.out.println("\n共" + count + "个数字符合要求");
    }

    /**
     * 检查3和5是否相邻
     * @param num 待检查的6位数字
     * @return 3和5相邻则返回true
     */
    static boolean check35(int num) {
    int temp = num%10;
    while(num > 100) {
    num /= 10;
    if(temp == 3) {
    if(num%10 == 5) {
    return true;
    }
    } else if(temp == 5) {
    if(num%10 == 5) {
    return true;
    }

    temp = num%10;
    }
    return false;
    }

    /**
     * 检查4是否在第三位
     * @param num 待检查的6位数字
     * @return true 表示4在第三位
     */
    static boolean check4(int num) {
    num /= 1000;
    return num%10 == 4;
    }

    /**
     * 检查一个6位数是否是1、2、3、4、5、6组成
     * @param num 待检查的6位数字
     * @return 如果是 1、2、3、4、5、6组成,则返回true
     */
    static boolean check6(int num) {
    int temp;
    while(num > 10) {
    temp = num%10;
    if(temp==0 || temp>6) {
    return false;
    }
    num /= 10;
    }
    return true;
    }

    /**
     * 检查数字是否重复
     * @param num
     * @return 如果有数字重复,则返回true
     */
    static boolean checkSameNum(int num) {
    int[] arr = new int[6]; 
    for(int i=0; i<6; i++) {
    arr[i] = num%10;
    num /= 10;
    }

    Arrays.sort(arr);
    for(int i=0; i<5; i++) {
    if(arr[i] == arr[i+1]) {
    return true;
    }
    }
    return false;
    }}
      

  11.   

    太马虎了,数字写错了static boolean check35(int num) {
    int temp = num%10;
    int next;
    while(num > 10) {
    num /= 10;
    next = num%10;
    if((temp == 3 && next == 5) || (temp == 5 && next == 3)) {
    return true;
    }
    temp = next;
    }
    return false;
    }
      

  12.   

    public class  Test 
    {
    public static void main(String[] args) {
    int count=0;
    for(int i1 = 1;i1 <= 6;i1++)
    for(int i2 = 1;i2 <= 6;i2++)
    for(int i3 = 1;i3 <= 6;i3++)
    for(int i4 = 1;i4 <= 6;i4++)
    for(int i5 = 1;i5 <= 6;i5++)
    for(int i6 = 1;i6 <= 6;i6++){
    if(i1 != i2 && i1 != i3 && i1 != i4 && i1 != i5 && i1 != i6){
    if(i2 != i3 && i2 != i4 && i2 != i5 && i2 != i6 ){
    if(i3 != i4 && i3 != i5 && i3 != i6){
    if(i4 != i5 && i4 != i6){
    if(i5 != i6){
    if(i3 != 4){
    int arr[] ={0,i1,i2,i3,i4,i5,i6,0};
    for(int j = 1;j <= 6;j++){
    if(arr[j] == 3 && (arr[j-1] != 5 && arr[j+1] !=5 )){
    System.out.print(""+i1 + i2 + i3 +i4 +i5 +i6+"\t");
    count++;
    if(count%10 == 0){
    System.out.println("");
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    System.out.println("总数是:" + count);
    }

    输出结果:
    123456 123465 123645 123654 125436 125463 125634 125643 126345 126543
    132456 132465 132546 132564 132645 132654 136245 136254 136425 136452
    136524 136542 142365 142563 143256 143265 143625 143652 145236 145263
    145623 145632 146325 146523 152346 152364 152436 152463 152634 152643
    156234 156243 156324 156342 156423 156432 162345 162543 163245 163254
    163425 163452 165234 165243 165423 165432 213456 213465 213645 213654
    215436 215463 215634 215643 216345 216543 231456 231465 231546 231564
    231645 231654 236145 236154 236415 236451 236514 236541 241365 241563
    243156 243165 243615 243651 245136 245163 245613 245631 246315 246513
    251346 251364 251436 251463 251634 251643 256134 256143 256314 256341
    256413 256431 261345 261543 263145 263154 263415 263451 265134 265143
    265413 265431 312456 312465 312546 312564 312645 312654 315246 315264
    315426 315462 315624 315642 316245 316254 316425 316452 316524 316542
    321456 321465 321546 321564 321645 321654 325146 325164 325416 325461
    325614 325641 326145 326154 326415 326451 326514 326541 341256 341265
    341526 341562 341625 341652 342156 342165 342516 342561 342615 342651
    345126 345162 345216 345261 345612 345621 346125 346152 346215 346251
    346512 346521 361245 361254 361425 361452 361524 361542 362145 362154
    362415 362451 362514 362541 365124 365142 365214 365241 365412 365421
    412365 412563 413256 413265 413625 413652 415236 415263 415623 415632
    416325 416523 421365 421563 423156 423165 423615 423651 425136 425163
    425613 425631 426315 426513 431256 431265 431526 431562 431625 431652
    432156 432165 432516 432561 432615 432651 436125 436152 436215 436251
    436512 436521 451236 451263 451326 451362 451623 451632 452136 452163
    452316 452361 452613 452631 456123 456132 456213 456231 456312 456321
    461325 461523 462315 462513 463125 463152 463215 463251 465123 465132
    465213 465231 512346 512364 512436 512463 512634 512643 513246 513264
    513426 513462 513624 513642 516234 516243 516324 516342 516423 516432
    521346 521364 521436 521463 521634 521643 523146 523164 523416 523461
    523614 523641 526134 526143 526314 526341 526413 526431 541236 541263
    541326 541362 541623 541632 542136 542163 542316 542361 542613 542631
    543126 543162 543216 543261 543612 543621 546123 546132 546213 546231
    546312 546321 561234 561243 561324 561342 561423 561432 562134 562143
    562314 562341 562413 562431 563124 563142 563214 563241 563412 563421
    612345 612543 613245 613254 613425 613452 615234 615243 615423 615432
    621345 621543 623145 623154 623415 623451 625134 625143 625413 625431
    631245 631254 631425 631452 631524 631542 632145 632154 632415 632451
    632514 632541 641325 641523 642315 642513 643125 643152 643215 643251
    645123 645132 645213 645231 651234 651243 651324 651342 651423 651432
    652134 652143 652314 652341 652413 652431 总数是:396
    写得有些复杂,不知对不对,请大家指出!
      

  13.   

    import java.util.*;public class Permutation {
    public static void main(String[] args) {
    //1,2,3,4,5,6六个数字,4不能在第三位,3,5不能相邻,打印所有排列。
    List<Integer> datas = new ArrayList<Integer>();
    for(int i = 123456; i<=654321; i++) {
    String s = String.valueOf(i);
    if(s.matches("[1-6]{6}") && !s.matches("[1-6]{2}4[1-6]{3}") && !s.matches("^[1-6]*35[1-6]*$||^[1-6]*53\\[1-6]*$")) {
    Set<Character> set = new HashSet<Character>();
    for(int j = 0 ;j<s.length(); j++){
    char ch = s.charAt(j);
    set.add(Character.valueOf(ch));
    if(set.size() == s.length()) {
    datas.add(Integer.parseInt(s));
    }
    }
    }
    }
    System.out.println(datas.size());  // ----output : 498
    }
    }为什么我的输出结果是这么多
      

  14.   

    import java.util.*;public class Permutation {
    public static void main(String[] args) {
    //1,2,3,4,5,6六个数字,4不能在第三位,3,5不能相邻,打印所有排列。
    List<Integer> datas = new ArrayList<Integer>();
    for(int i = 123456; i<=654321; i++) {
    String s = String.valueOf(i);
    if(s.matches("[1-6]{6}") && !s.matches("[1-6]{2}4[1-6]{3}") && !s.matches("^[1-6]*35[1-6]*$||^[1-6]*53[1-6]*$")) {
    Set<Character> set = new HashSet<Character>();
    for(int j = 0 ;j<s.length(); j++){
    char ch = s.charAt(j);
    set.add(Character.valueOf(ch));
    if(set.size() == s.length()) {
    datas.add(Integer.parseInt(s));
    }
    }
    }
    }
    System.out.println(datas.size()); //----output : 396 
    }
    }
    晕!,正则表达式写错了,应该是396
      

  15.   

    <code>
    public static void f() {
    int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
    int[] b = new int[6];
    Random r = new Random();
    for (int i = 0; i < a.length; i++) {
    int m = r.nextInt(6);
    while (i == 3 && m == 4) {
    m = r.nextInt(6);
    }
    b[i] = a[m];
    for (int k = 0; k < i; k++) {
    while (i != 0 && b[i] == 0) {
    m = r.nextInt(6);
    b[i] = a[m];
    while (b[i - 1] == 3 && b[i] == 5 || b[i - 1] == 5
    && b[i] == 3) {
    m = r.nextInt(6);
    b[i] = a[m];
    }
    }
    }
    a[m] = 0;
    }
    for (int o : b) {
    System.out.println(o);
    }
    } public static void main(String args[]) {
    f();
    }
    </code>