面试题目记录

来源:互联网 发布:试衣间软件 编辑:程序博客网 时间:2024/06/03 01:16

用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

方法一:

public class Test3{    public static void main(String[] args)    {        int number = 0;        int count = 0;        String numberStr;        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; d <= 5; d++)                    {                        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)                            {                                number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;                                numberStr = String.valueOf(number);                                // 4不能再第3位, "3"与"5"不能相连                                if (numberStr.indexOf("4") != 2 && numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1)                                {                                    count++;                                    System.out.println(numberStr);                                }                            }                        }                    }                }            }        }        System.out.println("count=" + count);    }}


方法二:

public class Test3{    public static void main(String[] args)    {        int number = 0;        int count = 0;        String numberStr;        for (int a = 1; a <= 5; a++)        {            for (int b = 1; b <= 5; b++)            {            int c=1;            while(c<=5){                    for (int d = 1; d <= 5; d++)                    {                        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)                            {                                number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;                                numberStr = String.valueOf(number);                                // 4不能再第3位, "3"与"5"不能相连                                if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1)                                {                                    count++;                                    System.out.println(numberStr);                                }                            }                        }                    }                c++;                if(c==4) c++;            }            }        }        System.out.println("count=" + count);    }


方法三:

package com.lh.test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo{public static void main(String[] args){ long start = System.currentTimeMillis();Test1();long end = System.currentTimeMillis();System.out.println("你的运行时间:" + (long)(end - start));start = System.currentTimeMillis();System.out.println("开始时间:" + System.currentTimeMillis());Test();System.out.println("结束时候用时:" + (long)(System.currentTimeMillis() - start));}public static void Test1(){int number = 0;int count = 0;String numberStr;for (int a = 1; a <= 5; a++){for (int b = 1; b <= 5; b++){int c=1;while(c<=5){for (int d = 1; d <= 5; d++){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){number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;numberStr = String.valueOf(number);// 4不能再第3位, "3"与"5"不能相连if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1){count++;System.out.println(numberStr);}}}}c++;if(c==4) c++;}}}System.out.println("count=" + count);}public static void Test(){int number = 0;int count = 0;String numberStr;for (int a = 1; a <= 5; a++){for (int b = 1; b <= 5; b++){int c=1;if(a == b){continue;}while(c<=5){for (int d = 1; d <= 5; d++){if(a == d || b == d){continue;}for (int e = 1; e <= 5; e++){if(a == e || b == e || d == e){continue;}// 打印出所有不重复的数字if (a != c && b != c && c != d && c != e){number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;numberStr = String.valueOf(number);// 4不能再第3位, "3"与"5"不能相连if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1){count++;System.out.println(numberStr);}}}}c++;if(c==4) c++;}}}System.out.println("count=" + count);}}


方法四:

    public static int n = 0;public static void main(String[] args) {List<String> list = new LinkedList<String>(Arrays.asList("1","2","3","4","5"));listAll(list, "");System.out.println(n);    }    public static void listAll(List<String> list, String strNum) {        if (list.isEmpty()) {        n++;            System.out.println(strNum);        }        for (int i = 0; i < list.size(); i++) {        int len = strNum.length();        List<String> tmp = new LinkedList<String>(list);        String c = tmp.remove(i);        if (len == 2 && c.charAt(0) == '4'   // 第三个位置不能为4            || (len > 0 && c.charAt(0) == '3' && strNum.charAt(strNum.length() - 1) == '5')    // 3和5不相连            || (len > 0 && c.charAt(0) == '5' && strNum.charAt(strNum.length() - 1) == '3')) {        continue;        }            listAll(tmp, strNum + c);        }    }


 

0 0
原创粉丝点击