posts - 195, comments - 34, trackbacks - 0, articles - 1
          算法利用了從1到pow(2,n)-1的所有整數(shù)的二進(jìn)制表示中1的位置組合剛好是所要取的所有組合的性質(zhì)。  

           1static void combin(char []list)
           2    {
           3        int count = (int)Math.pow(2, list.length)-1 ;
           4        int [] b = new int[list.length];
           5        for(int i = 1; i <= count;i++)
           6        {
           7            
           8            for(int j =0 ;j < list.length;j++)
           9            {
          10                b[j] = (i>>j) & 1;    
          11            }

          12            for(int k = 0;k < list.length;k++)
          13            {
          14                if(b[k] == 1)
          15                    System.out.print(list[k]);
          16            }

          17        //    if(i < count) System.out.print(", ");
          18            System.out.println();
          19        }

          20    }
          一個(gè)輸出結(jié)果是,如此調(diào)用
          char []str={'1''2''3''4'};
            
            combin(str);
          結(jié)果如下:
          1
          2
          12
          3
          13
          23
          123
          4
          14
          24
          124
          34
          134
          234
          1234



          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 林芝县| 清流县| 黔南| 肥城市| 新干县| 浑源县| 新田县| 安西县| 大英县| 黄龙县| 集安市| 温州市| 寻乌县| 武平县| 永泰县| 东阿县| 乐清市| 仙游县| 象山县| 巍山| 永川市| 永春县| 乌海市| 广南县| 涟源市| 平泉县| 崇州市| 无锡市| 赫章县| 常山县| 潼南县| 乌拉特后旗| 古田县| 汉中市| 邯郸市| 金华市| 桓台县| 安阳市| 贵州省| 库车县| 油尖旺区|