posts - 495,comments - 227,trackbacks - 0
          package com.test;

          import java.util.ArrayList;
          import java.util.List;

          /**
           * 1、2、2、3、4、5這六個數字,用java寫一個main函數,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"與"5"不能相連.
           * 
           * 
          @author SIMONE wangxinsh55@126.com 2009-6-12
           
          */
          public class GroupTest {
              
          public static List<String> list = new ArrayList<String>();

              
          /**
               * 構造字符串的所有排序組合
               * 
               * 
          @param str 將要組合成的字符
               * 
          @param nstr 源字符串集
               
          */
              
          public static void group(String str, String nstr) {
                  
          if (str.length() != nstr.length()) {
                      String rest 
          = getRest(str, nstr);
                      
          for (int i = 0; i < rest.length(); i++) {
                          String temp 
          = str + rest.substring(i, i + 1);
                          
          if (temp.indexOf("4"!= 2 && temp.indexOf("35"== -1 && temp.indexOf("53"== -1) {// 過濾顯示條件,如果去掉此處的判斷,就是列出所有字符集的排列組合
                              System.out.println(temp);
                              
          if (!list.contains(temp)) {
                                  list.add(temp);
                              }
                              group(temp, nstr);
                          }
                      }
                  }
              }

              
          /**
               * 從源字符串集中去除將要組合成的字符
               * 
               * 
          @param str 將要組合成的字符
               * 
          @param nstr 源字符串集
               * 
          @return 剩余字符串集
               
          */
              
          public static String getRest(String str, String nstr) {
                  String rest 
          = "";
                  
          if (nstr.length() > str.length()) {
                      rest 
          = nstr;
                      
          for (int i = 0; i < str.length(); i++) {
                          rest 
          = rest.replaceFirst(str.substring(i, i + 1), "");// 注意此處的replaceFirst,而不是replaceAll
                      }
                  }
                  
          return rest;
              }

              
          public static void main(String[] args) {
                  group(
          """122345");
                  System.out.println(list.toString());
              }
          }
          posted on 2009-06-12 15:54 SIMONE 閱讀(4217) 評論(2)  編輯  收藏 所屬分類: JAVA

          FeedBack:
          # re: 1、2、2、3、4、5這六個數字,用java寫一個main函數,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"與"5"不能相連.
          2009-06-12 17:14 | Ken Wu
          god...  回復  更多評論
            
          # re: 1、2、2、3、4、5這六個數字,用java寫一個main函數,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"與"5"不能相連.
          2009-06-14 19:01 | 愛吃魚頭
          用經典的排列算法,在輸出時判斷一下就可以了。
          不明白為什么要用字符串操作呢。  回復  更多評論
            
          主站蜘蛛池模板: 岑巩县| 三河市| 新竹县| 乌苏市| 兖州市| 洪雅县| 安龙县| 汝南县| 西安市| 日喀则市| 华容县| 嵩明县| 白水县| 克拉玛依市| 武宣县| 汶川县| 衡阳市| 澄迈县| 雷州市| 定远县| 周宁县| 吉水县| 瑞金市| 河间市| 雅安市| 平果县| 上饶县| 扶风县| 瑞金市| 墨竹工卡县| 始兴县| 博湖县| 泰州市| 南澳县| 阿合奇县| 弋阳县| 中山市| 南木林县| 望都县| 博兴县| 麻江县|