當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          package com.heyang;

          /**
           * 諸葛亮要派出五虎上將中的三員執行任務,請列出所有可能的組合
           * 
          @author 何楊(heyang78@gmail.com)
           *
           * 
          @since 2009-2-11 上午08:29:25
           * 
          @version 1.00
           
          */

          public class Combiner {
              
          static char[] arr={'','','','',''};

              
          public static void main(String[] args) {
                  
          int[] arr = new int[3];
                  combine(
          53, arr);
              }


              
          public static void combine(int total, int chooseCount, int selectedArr[]) {
                  
          for (int i = total; i >= chooseCount; i--){
                      selectedArr[chooseCount 
          - 1= i - 1;
                      
                      
          if (chooseCount > 1){
                          combine(i 
          - 1, chooseCount - 1, selectedArr);
                      }

                      
          else 
                      
          {
                          
          for (int j = selectedArr.length - 1; j >= 0; j--{
                              System.out.print(arr[selectedArr[j]] 
          + ",");
                          }

                          System.out.println();
                      }

                  }

              }

          }


          結果:
           1黃,馬,趙,
           2黃,馬,張,
           3黃,馬,關,
           4黃,趙,張,
           5黃,趙,關,
           6黃,張,關,
           7馬,趙,張,
           8馬,趙,關,
           9馬,張,關,
          10趙,張,關,

          排列代碼:
          package com.heyang;

          /**
           * 全排列代碼
           * 趙錢孫李四人排隊,求所有排隊方案
           * 
           * 
          @author 何楊(heyang78@gmail.com)
           *
           * 
          @since 2009-2-11 下午01:26:45
           * 
          @version 1.00
           
          */

          public class Permutation{
              
          public static void main(String[] args){
                  Character[] arr
          ={'','','',''};
                  permutation(arr,
          0,arr.length);
              }

              
              
          public static void permutation(Object[] arr,int start,int end){
                  
          if(start<end+1){
                      permutation(arr,start
          +1,end);
                      
                      
          for(int i=start+1;i<end;i++){
                          Object temp;
                          
                          temp
          =arr[start];
                          arr[start]
          =arr[i];
                          arr[i]
          =temp;
                          
                          permutation(arr,start
          +1,end);
                          
                          temp
          =arr[i];
                          arr[i]
          =arr[start];
                          arr[start]
          =temp;
                      }

                  }

                  
          else{
                      
          for(int i=0;i<end;i++){
                          System.out.print(arr[i]);
                      }

                      System.out.print(
          "\n");
                  }

              }

          }


          排列結果:
           1趙錢孫李
           2趙錢李孫
           3趙孫錢李
           4趙孫李錢
           5趙李孫錢
           6趙李錢孫
           7錢趙孫李
           8錢趙李孫
           9錢孫趙李
          10錢孫李趙
          11錢李孫趙
          12錢李趙孫
          13孫錢趙李
          14孫錢李趙
          15孫趙錢李
          16孫趙李錢
          17孫李趙錢
          18孫李錢趙
          19李錢孫趙
          20李錢趙孫
          21李孫錢趙
          22李孫趙錢
          23李趙孫錢
          24李趙錢孫
          posted on 2009-02-11 08:32 何楊 閱讀(274) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 灵宝市| 舞钢市| 长顺县| 济阳县| 辽宁省| 灵武市| 集贤县| 陆川县| 五大连池市| 比如县| 怀仁县| 屯留县| 敖汉旗| 红原县| 都安| 定边县| 云林县| 中卫市| 平利县| 宁乡县| 沐川县| 福鼎市| 和林格尔县| 博客| 银川市| 武陟县| 凌源市| 田林县| 丽水市| 延川县| 平遥县| 新宁县| 德令哈市| 宁津县| 商洛市| 皮山县| 阿城市| 泰顺县| 永州市| 徐闻县| 禹城市|