题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,
  如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
有思路的留下你的代码,大家比较下。

解决方案 »

  1.   

    http://blog.csdn.net/hudie1234567/archive/2010/09/26/5908499.aspx
      

  2.   

    http://blog.csdn.net/sunyujia/archive/2009/04/26/4124011.aspx对照着自己弄弄满足需求吧搜索关键字:Java 全排列
      

  3.   

    import java.util.Iterator;  
    import java.util.Set;  
    import java.util.TreeSet;  
    public class Combination {  
        public static void main(String[] args) {  
            int[] num = new int[6];  
            for (int i=0; i<num.length; i++) {  
                num[i] = i + 1;  
            }  
              
            long n;  
            Set<Long> result = new TreeSet<Long>();  
            Set<String> result2 = new TreeSet<String>();  
              
            //做1-6六个数字的全排列,过滤掉4在第三位的情况  
            for(int a=0; a<num.length; a++) {  
                for(int b=0; b<num.length; b++) {  
                    //在进入里层循环前判断是否符合条件,可以减少循环次数,  
                    //提交效率  
                    if(a == b) {  
                        continue;  
                    }  
                    for(int c=0; c<num.length; c++) {  
                        if(c == a || c == b || c == 3) {  
                            continue;  
                        }  
                        for(int d=0; d<num.length; d++) {  
                            if(d == a || d == b || d == c) {  
                                continue;  
                            }  
                            for(int e=0; e<num.length; e++) {  
                                if(e == a || e == b || e == c || e == d) {  
                                    continue;  
                                }  
                                for(int f=0; f<num.length; f++) {  
                                    if(f == a || f == b || f == c || f == d || f == e) {  
                                        continue;  
                                    }  
                                    n = num[a] * 100000 + num[b] * 10000 + num[c] * 1000 + num[d] * 100 + num[e] * 10 + num[f];  
                                    result.add(n);  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
              
            //从上面的结果集中找到3和5不相邻的,放入到新的TreeSet中,放入前用2替换掉6  
            String temp;  
            Iterator<Long> iterator = result.iterator();  
            while(iterator.hasNext()) {  
                temp = iterator.next() + "";  
                if(temp.indexOf("35") == -1 && temp.indexOf("53") == -1) {  
                    result2.add(temp.replace("6", "2"));  
                }   
            }  
            //打印结果  
            System.out.println("满足条件的记录数(一行显示10个):" + result2.size());  
            int count = 0;  
            for(String s : result2) {  
                System.out.print(s + "    ");  
                count ++;  
                if(count % 10 == 0) {  
                    System.out.println();  
                }  
            }  
        }  
    }
      

  4.   


    public static void combination(int[] arr)
        {
            comCombination(arr, 0);
        }    private static void comCombination(int[] arr, int index)
        {
            if (index == arr.length - 1)
            {
             if(arr[arr.length - 1] == 5 && arr[arr.length - 2] == 3)
             return;
             if(arr[arr.length - 1] == 3 && arr[arr.length - 2] == 5)
             return;
                for (int i = 0; i < arr.length; i++)
                {
                   System.out.printf("%d ", arr[i]);
                }
                System.out.println();
                count++;
                return;
            }
            boolean notEc = false;
            for (int i = index; i < arr.length; i++)
            {
             if(arr[i] == 2 && notEc)
             continue;
             if(index == 2 && arr[i] == 4)
             continue;
             if(index > 0 && arr[index - 1] == 3 && arr[i] == 5)
             continue;
             if(index > 0 && arr[index - 1] == 5 && arr[i] == 3)
             continue;
             if(arr[i] == 2)
             notEc = true;
                int temp = arr[index];
                arr[index] = arr[i];
                arr[i] = temp;
                comCombination(arr, index + 1);
                temp = arr[index];
                arr[index] = arr[i];
                arr[i] = temp;
            }
        }
      

  5.   

    呵呵
    这题老早前有了package CSDN;import java.util.*;/**
     * 用1、2、2、3、4、5这六个数字,打印出所有不同的排列,
     * 如:512234、412345等,要求: "4 "不能在第三位, "3 "与 "5 "不能相连。
     * 
     * @author xqh
     *
     */
    public class FullSortTest {
    private static int count = 0; public static void main(String[] args) {
    long time = System.currentTimeMillis();
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    List<Integer> list = new ArrayList<Integer>();
    for (int i : arr)
    list.add(i);
    fullSort(list, new ArrayList<Integer>());
    System.out.println("一共" + count + "种排列");
    System.out.println(System.currentTimeMillis() - time + "ms");
    } private static void fullSort(List<Integer> date, List<Integer> target) {
    if (target.size() == 6) {
    boolean flag = true;
    for (int i = 0; i < target.size(); i++) { // 判断数列中的数是否符合题意
    if (i + 1 < target.size())
    if (target.get(2) == 4
    || target.get(i) + target.get(i + 1) == 8)
    flag = false;
    }
    if (flag) {
    count++;
    for (int i : target)
    System.out.print(i + " ");
    } else
    return;
    System.out.println();
    return;
    }
    boolean isRe = false;
    for (int i = 0; i < date.size(); i++) {
    if (date.get(i) == 2 && isRe)
    continue;
    if (date.get(i) == 2)
    isRe = true;
    List<Integer> newDate = new ArrayList<Integer>(date);
    List<Integer> newTarget = new ArrayList<Integer>(target);
    newTarget.add(newDate.get(i));
    newDate.remove(i);
    fullSort(newDate, newTarget);
    } }
    }
      

  6.   


    public class KingTieTest {
    public static void main(String[] args) {
    int[] c = {1,2,3,4,5};
    int count = 0;
    for(int i0:c){

    for(int i1:c){
    if(((i0 == 3) && (i1 == 5))|| ((i0 == 5) && (i1 ==3)))  continue;
    for(int i2:c){
    if(((i2 == 3) && (i1 == 5))|| ((i2 == 5) && (i1 ==3)) || (i2 == 4)) continue;
    for(int i3:c){
    if(((i2 == 3) && (i3 == 5))|| ((i2 == 5) && (i3 ==3)) ) continue;
    for(int i4:c){
    if(((i4 == 3) && (i3 == 5))|| ((i4 == 5) && (i3 ==3))) continue;
    System.out.println("   " + i0+i1+i2+i3+i4);
    count ++;
    }
    }
    }
    }
    }
    System.out.println("count=" + count);
    }
    }//不是看了上面说要写在main里就把判断3和5的部分放到一个方法是里了
      

  7.   

    package xu;public class Test {
    public static void main(String args[]) {
    int sum = 0;
    int k = 0;
    for(int a = 1; a <= 5; a++) {
    for(int b = 1; b <= 5; b++) {
    for(int c = 1; c <= 5; c++) {
    for(int d = 1; c <=5; c++) {
    for(int e = 1; e <= 5; e++) {
    if(a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c!=e&&d!=e&&c != 4) {
    sum = a *10000 + b * 1000 + c * 100 + d * 10 + e;
    k = sum;
    while(sum >= 10) {
    if((sum %10 == 3 && (sum /10)%10==5) ||(sum%10==5&&(sum/10)%10==3)) {

    break;
    }
    sum = sum /10;

    }
    if(sum < 10)
    System.out.println(k);

    }
    }
    }
    }
    }
    }
    }
    }
      

  8.   


    public class TestDemo01 {

    public static void print(int x){
    System.out.print(x+",");
    }
    public static void swap(int[] s,int i,int j){
    if(i!=j){
    int temp = s[i];
    s[i] = s[j];
    s[j] = temp;
    }
    }
    //递归实现数组的全排列
    public static void arrange(int[] s,int st,int len){
    if(st==len-1){
    int j=0;
    boolean flag = true;
    for(j=0;j<len-1;j++){
    //如果3,5相连则不满足条件
    if((s[j]==3&&s[j+1]==5)||(s[j]==5&&s[j+1]==3))
    flag = false;
    //如果第三个数是4也不满足条件
    if(s[j]==4&&j==2)
    flag = false;
    }
    //满足条件打印输出
    if(flag){
    for(int i=0;i<len;i++){
    print(s[i]);
    }
    System.out.println();
    }
    }else{
    for(int i=st;i<len;i++){
    swap(s,st,i);
    arrange(s,st+1,len);
    swap(s,st,i);
    }
    }
    }
    public static void main(String[] args) {
    int[] s = {1,2,2,3,4,5};
    arrange(s,0,6);
    }
    }一起学习学习
      

  9.   

    public class TestDemo01 {

    public static void print(int x){
    System.out.print(x+",");
    }
    public static void swap(int[] s,int i,int j){
    if(i!=j){
    int temp = s[i];
    s[i] = s[j];
    s[j] = temp;
    }
    }
    //递归实现数组的全排列
    public static void arrange(int[] s,int st,int len){
    if(st==len-1){
    int j=0;
    boolean flag = true;
    for(j=0;j<len-1;j++){
    //如果3,5相连则不满足条件
    if((s[j]==3&&s[j+1]==5)||(s[j]==5&&s[j+1]==3))
    flag = false;
    //如果第三个数是4也不满足条件
    if(s[j]==4&&j==2)
    flag = false;
    }
    //满足条件打印输出
    if(flag){
    for(int i=0;i<len;i++){
    print(s[i]);
    }
    System.out.println();
    }
    }else{
    for(int i=st;i<len;i++){
    swap(s,st,i);
    arrange(s,st+1,len);
    swap(s,st,i);
    }
    }
    }
    public static void main(String[] args) {
    int[] s = {1,2,2,3,4,5};
    arrange(s,0,6);
    }
    }
      

  10.   


    /*  
    该公司笔试题就1个,要求在10分钟内作完。    
      题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。  
    请问这题怎么做呢   
    */  
      
    /*  
    构思:这里需要先将其中一个 2 当成一个特殊的数字 6 来处理,然后在排除重复的情况,采用正则处理.  
    */  
      
    //package com.ricky.www;   
      
    import java.util.regex.Matcher;   
    import java.util.regex.Pattern;   
    import java.util.LinkedHashSet;   
      
    public class Test2{   
        public static void main(String[] args){   
            LinkedHashSet<String> set = process();   
            int ln = 0;   
            for(String str : set){   
                   
                System.out.printf("%-7s",str);   
      
                if(++ln % 10 == 0){   
                    System.out.println();   
                }   
      
            }   
      
            System.out.println();   
            System.out.println("Total: " + set.size());   
        }   
      
        private static LinkedHashSet<String> process(){   
      
            LinkedHashSet<String> set = new LinkedHashSet<String>();   
      
            String regex1 = "([1-6])(?!\\1)([1-6])(?!\\1|\\2|4)([1-6])(?!\\1|\\2|\\3)([1-6])(?!\\1|\\2|\\3|\\4)([1-6])(?!\\1|\\2|\\3|\\4|\\5)([1-6])";   
            String regex2 = "35";   
            Pattern pattern1 = Pattern.compile(regex1);   
            Pattern pattern2 = Pattern.compile(regex2);   
            Matcher matcher = null;   
      
            int ln = 0;   
      
            for(int i = 123456 ; i <= 654321 ; i ++){   
                matcher = pattern1.matcher(i + "");   
                   
                //测试不重复   
                if(!matcher.matches()){   
                    continue;   
                }   
      
                //测试35不相连   
                matcher = pattern2.matcher(i + "");   
      
                if(matcher.find()){   
                    continue;   
                }   
                   
                String element = i + "";   
                element = element.replace("6","2");   
      
                set.add(element);   
      
            }   
      
            return set;   
        }   
    }  
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/micsolaris/archive/2010/07/08/5721859.aspx
      

  11.   

    用PHP实现了一个:$handle = fopen("num.txt","w");
    for($i0=1;$i0<6;$i0++){
    for($i1=1;$i1<6;$i1++){
    for($i2=1;$i2<6;$i2++){
    for($i3=1;$i3<6;$i3++){
    for($i4=1;$i4<6;$i4++){
    $num = $i0.$i1.$i2.$i3.$i4;
    if(preg_match("/[\d]{2}[^4]{1}[\d]{2}/",$num)){
    if(!preg_match("/35/",$num)){
    fwrite($handle,$num."\r\n");
    }
    }
    }
    }
    }
    }
    }
    fclose($handle);
      

  12.   

    用正则写了个这样的public class Test8 {
    public static void main(String[] args) {
    test[] a = new test[6];
    a[0] = new test(1);
    a[1] = new test(2);
    a[2] = new test(2);
    a[3] = new test(3);
    a[4] = new test(4);
    a[5] = new test(5);
    List<String> l = new ArrayList<String>();
    test[] b = new test[6];
    for(int i = 0; i < a.length;i++) {
    int num = 0;
    if(a[i].f){
    b[num] = a[i];
    a[i].setf(false);
    m(a,b,++num,l);
    }
    a[i].setf(true);
    }
    int n = 0;
    Pattern p = Pattern.compile("\\d\\d[^4]\\d+");
    Pattern p2 = Pattern.compile("3(?:5)|5(?:3)");
    for(int i = 0; i < l.size(); i++) {
    if(l.get(i).matches(p.pattern())) {
    Matcher m = p2.matcher(l.get(i));
    if(!m.find()) {
    System.out.println(l.get(i).toString());
    n++;
    }
    }
    }
    System.out.println(n);
    }

    public static void m(test[] a,test[] b,int num,List<String> l) {
    if(num==a.length-1) {
    for(int j = 0; j < a.length;j++) {
    if(a[j].f){
    b[num] = a[j];
    }
    }
    String str = new String("");
    for(int i = 0; i < b.length;i++) {
    str = str+b[i].getID();
    }
    l.add(str);
    return;
    }
    else {
    for(int j = 0; j < a.length;j++) {
    if(a[j].f){
    b[num] = a[j];
    a[j].setf(false);
    m(a,b,++num,l);
    a[j].setf(true);
    --num;
    }
    }
    }
    return;
    }
    }class test {
    int id;
    boolean f = true;
    public test(int id) {
    this.id  = id;
    }
    public int getID() {
    return id;
    }
    public void setf(boolean f) {
    this.f = f;
    }
    }
      

  13.   

    公司考这道题的目的不是看你会不会,主要是看你的算法。楼上很多都用了多从for循环,这样就导致结果实在不敢恭维。要用比较简单的算法,在短时间内出结果。
      

  14.   


    package com;public class demo
    {
    public static void main(String []ages)
    {
    demo ks=new demo();
    }
    public demo()
    {
    for(int i=1;i<6;i++)
    {

    for(int a=1;a<6;a++)
    {

    for(int b=1;b<6;b++)
    {

    for(int c=1;c<6;c++)
    {
    for(int d=1;d<6;d++)
    {
    if(b!=4&&i+a!=8&&a+b!=8&&b+c!=8&&c+d!=8)
    {
    System.out.print(""+i+a+b+c+d+",");

    }
    }
    }

    }
    }
    }
    }
    }