• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 21,  comments - 9,  trackbacks - 0
            此題是一個排列組合加遞歸的題目。具體解釋看代碼里面的注釋:
              1 #include<stdio.h>  
              2 int x,y,k;  
              3 int array[36][36];  
              4 int result[31];  
              5 //初始化楊輝三角  
              6 void init_array()  
              7 {  
              8     array[0][0] = 1;  
              9     for(int i = 1;i < 31;++i)  
             10     {  
             11         for(int j = 0;j <= i;++j)  
             12         {  
             13             if(j == 0 ||j == i)  
             14             {  
             15                 array[i][j] = 1;  
             16             }  
             17             else  
             18             {  
             19                 array[i][j] = array[i - 1][j - 1] + array[i- 1][j];  
             20             }  
             21         }  
             22     }  
             23 }  
             24   
             25 int get_num_count(int num)  
             26 {  
             27     int total = 0;  
             28     while(num > 0)  
             29     {  
             30         ++total;  
             31         num /= 2;  
             32     }  
             33     return total;  
             34 }  
             35   
             36 //去掉首位的1  
             37 int delete_highest(int num)  
             38 {  
             39     int count = get_num_count(num);  
             40     int total = 1<<(count - 1);  
             41     total = ~total;  
             42     return num & total;  
             43 }  
             44   
             45 //計算跟一個數2進制位數相同的最大值  
             46 int getMin(int total_nums,int one_nums)  
             47 {  
             48     int total = 0;  
             49     if(total_nums < one_nums)  
             50         return -1;  
             51     for(int i = 0;i < one_nums;++i)  
             52     {  
             53         total = total * 2 + 1;  
             54     }  
             55     for(int j = one_nums;j < total_nums;++j)  
             56     {  
             57         total *= 2;  
             58     }  
             59     return total;  
             60 }  
             61 //計算跟一個數2進制位數相同的最小值  
             62 int getMax(int total_nums,int one_nums)  
             63 {  
             64     int total = 1;  
             65     if(total_nums < one_nums)  
             66         return 1000000001;  
             67     for(int i = 0;i < total_nums - one_nums;++i)  
             68     {  
             69         total = total * 2;  
             70     }  
             71     for(int j = 1;j < one_nums;++j)  
             72     {  
             73         total = total * 2 + 1;  
             74     }  
             75     return total;  
             76 }  
             77   
             78   
             79 int recursive_deal(int start,int end,int one_nums)  
             80 {  
             81     if(start > end)  
             82         return 0;  
             83     int start_count = get_num_count(start);  
             84     int end_count = get_num_count(end);  
             85     if(one_nums == 0)  
             86     {  
             87         if(start == 0 || end == 0)  
             88             return 1;  
             89         else  
             90             return 0;  
             91     }  
             92     else  
             93     //如果始末兩個數的位數相同的話,就去掉最高位  
             94     if(start_count == end_count)  
             95     {  
             96             return recursive_deal(delete_highest(start),delete_highest(end),one_nums - 1);  
             97     }  
             98     else    //如果始末兩個數的位數不同的話,分別計算和起始,終止的的數的2進制位數,中間的數的數量可以通過排列組合計算出來  
             99     {  
            100         int total = 0;  
            101         for(int i = start_count + 1;i < end_count;++i)  
            102         {  
            103             total += array[i - 1][one_nums - 1];  
            104         }  
            105         return total + recursive_deal(start,getMin(start_count,one_nums),one_nums) + recursive_deal(getMax(end_count,one_nums),end,one_nums);  
            106     }  
            107 }  
            108   
            109 int main()  
            110 {  
            111     int tests;  
            112     init_array();  
            113     scanf("%d",&tests);  
            114     while(--tests >= 0)  
            115     {  
            116         scanf("%d%d%d",&x,&y,&k);  
            117         printf("%d\n",recursive_deal(x,y,k));  
            118     }  
            119     return 0;     
            120 }  
            posted on 2012-04-07 10:21 崔佳星 閱讀(166) 評論(0)  編輯 收藏 引用 所屬分類: xoj
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久九九免费高清视频| 亚洲午夜精品久久久久久人妖| 久久国产成人午夜aⅴ影院| 久久这里只精品国产99热| 久久天天躁狠狠躁夜夜不卡| 亚洲欧美一级久久精品| 人妻无码αv中文字幕久久| 麻豆精品久久精品色综合| 久久人人爽人人澡人人高潮AV| 久久国语露脸国产精品电影| 四虎国产精品免费久久久| 欧美久久久久久| 国产69精品久久久久99| 亚洲AV日韩精品久久久久| 久久国产精品二国产精品 | 久久er99热精品一区二区| 欧美激情精品久久久久| 久久精品视频一| 精品国产91久久久久久久a| 久久99精品久久久久久hb无码| 狠狠色综合网站久久久久久久| 人妻精品久久久久中文字幕69| 四虎国产精品成人免费久久| 久久亚洲国产中v天仙www| 久久精品国产网红主播| 亚洲αv久久久噜噜噜噜噜| 亚洲国产成人久久综合野外 | 青青草原综合久久大伊人精品| 2020国产成人久久精品| 久久免费99精品国产自在现线| 亚洲欧美日韩精品久久| 久久99精品久久久久久久不卡| 无码超乳爆乳中文字幕久久 | 久久精品国产亚洲AV无码麻豆| 亚洲精品高清一二区久久| 久久久久国产精品三级网| 青青热久久综合网伊人| 久久亚洲精品中文字幕三区| 久久99热精品| 国产女人aaa级久久久级| 久久精品不卡|