• <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 崔佳星 閱讀(172) 評論(0)  編輯 收藏 引用 所屬分類: xoj
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品美女久久久久av爽| 亚洲伊人久久大香线蕉综合图片| 久久99九九国产免费看小说| 久久e热在这里只有国产中文精品99| 狠狠色婷婷久久综合频道日韩| 亚洲精品tv久久久久| 免费精品国产日韩热久久| 婷婷久久久亚洲欧洲日产国码AV | 久久毛片一区二区| 中文字幕乱码人妻无码久久| 97久久精品午夜一区二区| 少妇被又大又粗又爽毛片久久黑人 | 久久―日本道色综合久久| 国产午夜精品久久久久九九| 久久天天躁狠狠躁夜夜2020一| 久久久久久夜精品精品免费啦| 品成人欧美大片久久国产欧美| 亚洲精品tv久久久久久久久久| 久久99国产综合精品| 久久国产成人午夜aⅴ影院| 一级a性色生活片久久无| 欧美综合天天夜夜久久| 亚洲色婷婷综合久久| 免费一级欧美大片久久网| 久久婷婷五月综合97色直播| 国产巨作麻豆欧美亚洲综合久久| 国产美女久久精品香蕉69| 国产亚州精品女人久久久久久 | 色综合久久综合网观看| 久久免费看黄a级毛片| 久久久WWW成人免费毛片| 国产高潮国产高潮久久久| 亚洲精品国产字幕久久不卡| 国产69精品久久久久观看软件| 国产AV影片久久久久久| 久久99精品久久只有精品| 久久久久亚洲精品日久生情| 亚洲精品午夜国产va久久| 综合久久一区二区三区 | 久久精品国产亚洲AV嫖农村妇女 | 66精品综合久久久久久久|