• <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 //計算跟一個數(shù)2進制位數(shù)相同的最大值  
             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 //計算跟一個數(shù)2進制位數(shù)相同的最小值  
             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     //如果始末兩個數(shù)的位數(shù)相同的話,就去掉最高位  
             94     if(start_count == end_count)  
             95     {  
             96             return recursive_deal(delete_highest(start),delete_highest(end),one_nums - 1);  
             97     }  
             98     else    //如果始末兩個數(shù)的位數(shù)不同的話,分別計算和起始,終止的的數(shù)的2進制位數(shù),中間的數(shù)的數(shù)量可以通過排列組合計算出來  
             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)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久天天躁狠狠躁夜夜躁2O2O| 亚洲精品乱码久久久久久蜜桃图片 | 日本国产精品久久| 国产精品毛片久久久久久久| 亚洲综合伊人久久大杳蕉| 99久久做夜夜爱天天做精品| 人人狠狠综合88综合久久| 欧美与黑人午夜性猛交久久久| 精品久久久无码中文字幕天天| 国产ww久久久久久久久久| 国产免费久久精品99久久| 99久久无码一区人妻| 国产高清国内精品福利99久久| 国产精品VIDEOSSEX久久发布| 91精品国产91久久| 久久www免费人成看国产片| 久久青青国产| 一本久久a久久精品亚洲| 蜜臀av性久久久久蜜臀aⅴ麻豆| 日本久久久久亚洲中字幕| 国产精品一久久香蕉国产线看观看| 色婷婷综合久久久久中文一区二区| 久久精品国产亚洲77777| 91精品国产高清久久久久久国产嫩草 | 久久99热只有频精品8| 国产精品免费看久久久| 中文字幕一区二区三区久久网站| 91久久福利国产成人精品| 亚洲国产高清精品线久久| 久久久噜噜噜www成人网| 99热成人精品免费久久| 日本WV一本一道久久香蕉| 成人妇女免费播放久久久| 久久久精品日本一区二区三区| 久久久高清免费视频| 久久精品视频网| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久99精品久久久久久hb无码 | 国产激情久久久久影院| 久久精品国产99国产精品亚洲| 久久国产高潮流白浆免费观看|