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

          真正的快樂來源于創造

            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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 民权县| 新晃| 延长县| 靖宇县| 汶川县| 江油市| 来凤县| 乐都县| 杭锦后旗| 鄂尔多斯市| 乳源| 铜陵市| 普兰店市| 图木舒克市| 牡丹江市| 临澧县| 当雄县| 达日县| 定边县| 贵南县| 冷水江市| 临颍县| 虞城县| 闵行区| 英吉沙县| 贵南县| 尚志市| 东城区| 襄樊市| 宁国市| 施秉县| 东至县| 衡南县| 山阴县| 漯河市| 鄢陵县| 大渡口区| 综艺| 五常市| 山东省| 获嘉县|