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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

要求:数字只能出现一次

  1. public class Test6 {

  2.         /**
  3.          * 用1、2、3、4这四个数字,用java写一个main函数,打印出所有不同的排列,如:1234、1243等,
  4.          * 要求:"4"不能在第一位,"1"与"3"不能相连。
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 //正则表达式,代表不以4开头的字符串
  9.                 String regex1 = "^[^4].*$";
  10.                
  11.                 //代表1和3相连的字符串
  12.                 String regex2 = ".*[1][3].*$";
  13.                 String regex3 = ".*[3][1].*$";

  14.                 //建立存储组合的字符
  15.                 StringBuilder sb = new StringBuilder("");
  16.                 String str = null;
  17.                 int count = 0;//统计个数
  18.                
  19.                 for(int i = 1;i < 5;i++){
  20.                         for (int j = 1        ; j < 5; j++) {
  21.                                 for(int k = 1; k < 5 ; k++){
  22.                                         for(int h = 1 ; h < 5 ; h++){
  23.                                                 sb.append(i).append(j).append(k).append(h);
  24.                                                 str = sb.toString();
  25.                                                 if(str.matches(regex1))//不以4开头
  26.                                                 {
  27.                                                         if(!(str.matches(regex2)||str.matches(regex3))){//1和3不相连
  28.                                                                 if(single(str)){ //过滤,只要数字不重复的组合
  29.                                                                         count++;
  30.                                                                         System.out.println(str+"..."+count);
  31.                                                                 }
  32.                                                         }
  33.                                                 }                               
  34.                                                 sb.delete(0,sb.length());
  35.                                         }
  36.                                 }
  37.                                
  38.                         }
  39.                 }
  40.                
  41.         }

  42.         private static boolean single(String str) {  //去除相同数字
  43.                 // TODO Auto-generated method stub
  44.                 boolean flag = true;
  45.                 for(int i = 1;i < 5;i++){
  46.                         int count = 0;
  47.                         int offset = 0;
  48.                         while((offset = str.indexOf(String.valueOf(i),offset))!=-1){
  49.                                 offset += 1;
  50.                                 count++;
  51.                                 if(count > 1){
  52.                                         flag = false;
  53.                                         break;
  54.                                 }
  55.                         }
  56.                 }
  57.                 return flag;
  58.         }

  59. }
复制代码


2 个回复

正序浏览
不错,学习了
回复 使用道具 举报
运行结果
  1. 1234...1
  2. 1243...2
  3. 1423...3
  4. 1432...4
  5. 2143...5
  6. 2341...6
  7. 3214...7
  8. 3241...8
  9. 3412...9
  10. 3421...10
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马