A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 何亚辉 中级黑马   /  2016-5-12 20:28  /  839 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class A {
        public static void main(String[] args){
        /*
        该公司笔试题就1个,要求在10分钟内作完。
    题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
请问这题怎么做呢
        */
        Z: for (int i =122345;i<=543221 ; i++) {
                int[] a = new int[6]; int s =0;
                for (int j =i;j>0 ;j/=10 ) {
                        a[s]=j%10;
                        s++;
                }
                int[] ar ={1,2,2,3,4,5};
                int sum =0;
                W: for (int x =0;x<=5 ;x++ ) {
                       
                         for (int y =0 ;y<=5 ;y++ ) {
                                if (a[x] ==ar[y]) {
                                        sum++;
                                        ar[y]=100;
                                        continue W;

                                }
                        }

                }
                if (sum ==6) {
                        if (a[3]!=4) {
                                for (int t =0;t<=4 ;t++ ) {
                                        if (a[t]==3||a[t]==5) {
                                                if (a[t+1]!=3&&a[t+1]!=5) {
                                                        System.out.println(i);
                                                        continue Z;
                                                }
                                        }
                               
                                }
                        }
                }
        }

        }
}

4 个回复

倒序浏览
本帖最后由 yanzhendong 于 2016-5-13 00:00 编辑

你的结果里有很多5和3相连的情况,按照你的思路,去掉了5和3相连的情况,结果是198个,这是我的代码:
  1. /**
  2.          * 题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412325等,要求:
  3.          * "4"不能在第三位,"3"与"5"不能相连。
  4.          */
  5.         public static void main(String[] args) {
  6.                 TreeSet<Integer> result = new TreeSet<>();
  7.                 for (int i = 122345; i <= 543221; i++) {
  8.                         String num = Integer.toString(i);
  9.                         if (num.toCharArray()[2] == '4' || num.contains("35") || num.contains("53")) {
  10.                                 continue;
  11.                         }

  12.                         int count = 0;
  13.                         int[] array = { 1, 2, 2, 3, 4, 5 };
  14.                         for (Character a : num.toCharArray()) {
  15.                                 for (int b = 0; b < 6; b++) {
  16.                                         if (array[b] == Integer.parseInt(a.toString())) {
  17.                                                 count++;
  18.                                                 array[b] = -1;
  19.                                                 break;
  20.                                         }
  21.                                 }
  22.                         }
  23.                         if (count == 6) {
  24.                                 result.add(new Integer(i));
  25.                         }
  26.                 }

  27.                 System.out.println("共有" + result.size() + "个");
  28.                 for (Integer item : result) {
  29.                         System.out.println(item);
  30.                 }
  31.         }
复制代码
回复 使用道具 举报
yanzhendong 发表于 2016-5-12 23:56
你的结果里有很多5和3相连的情况,按照你的思路,去掉了5和3相连的情况,结果是198个,这是我的代码: ...

恩恩,谢谢,,,刚刚看了下,,有一句话写错了,,continue Z; 应该在第二个if 语句的外面...
回复 使用道具 举报
lass A {
        public static void main(String[] args){
        /*
        该公司笔试题就1个,要求在10分钟内作完。
    题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
请问这题怎么做呢
        */
        Z: for (int i =122345;i<=543221 ; i++) {
                int[] a = new int[6]; int s =0;
                for (int j =i;j>0 ;j/=10 ) {
                        a[s]=j%10;
                        s++;
                }
                int[] ar ={1,2,2,3,4,5};
                int sum =0;
                W: for (int x =0;x<=5 ;x++ ) {
                       
                         for (int y =0 ;y<=5 ;y++ ) {
                                if (a[x] ==ar[y]) {
                                        sum++;
                                        ar[y]=100;
                                        continue W;

                                }
                        }

                }
                if (sum ==6) {
                        if (a[3]!=4) {
                                for (int t =0;t<=4 ;t++ ) {
                                        if (a[t]==3|| a[t]==5) {
                                                if (a[t+1]!=5&&a[t+1]!=3) {
                                                        System.out.println(i);
                                                       
                                                }
                                                continue Z;
                                        }
                                       
                                        }
                                }
                        }
               
        }

        }
}
回复 使用道具 举报
日常逛逛....
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马