面试题目记录

用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值