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

public class test3 {
    public static void main(String[] args) {
        for(int i=122345;i<=543221;i++){
            String x=i+"";
            char[] chars = x.toCharArray();
            List<String> list = new ArrayList<>();
            List<String> strList = new ArrayList<>();
            strList.add("1");
            strList.add("2");
            strList.add("2");
            strList.add("3");
            strList.add("4");
            strList.add("5");
            for (char aChar : chars) {
                list.add(aChar+"");
                }
//            获取2的索引确保2是有两个
            int i1 = list.indexOf("2");
            int i2 = list.lastIndexOf("2");
//            获取4的索引 判断不为2
            int i3 = list.lastIndexOf("4");
//            3 5 不能相连
            int i4 = list.lastIndexOf("3");
            int i5 = list.lastIndexOf("5");
            int abs = Math.abs(i4 - i5);
//新建的strList 包含122345  使用遍历得出的122345到543221 得出的所有包含12345的数字
            if(list.containsAll(strList)&&i1!=i2&&i3!=2&&abs!=1){

                System.out.println(list.get(0)+list.get(1)+list.get(2)+list.get(3)+list.get(4)+list.get(5));
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值