《劍指offer》43:n個骰子的點數

這道題其實也不難,遞歸的想法很簡單:

把目前n個骰子分成兩堆:1堆1個(可以枚舉點數1-6),1堆有n-1個。遞推到最後,n=0時,累計起來的點數的次數就+1啦

之所以寫這篇博客,是因爲我覺得書中的遞歸代碼寫得有點混亂(並沒有貶低作者的意思),所以貼一下我自己認爲比較好的寫法:

遞歸解法

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;

typedef unsigned int uInt;
const int low = 1, high = 6;

void prob(vector<int>& count, uInt n, int currentSum) {
    if (n == 0) {
        ++count[currentSum];
        return;
    }
    --n;
    for (int i = low; i <= high; ++i) {
        prob(count, n, currentSum+i);
    }
}

void cal(uInt n) {
    vector<int> count(high*n+1);
    prob(count, n, 0);
    int totalCount = pow(6, n), sum = 0;
    for (uInt i = n; i < count.size(); ++i) {
        sum += count[i];
        printf("點數爲%d的概率爲%.3lf, count=%d\n", 
        i, count[i]*1.0/totalCount, count[i]);
    }
    printf("sum=%d, totalCount=%d\n\n", sum, totalCount);
}

int main() {
    cal(1);
    cal(2);
    cal(3);

    return 0;
}

輸出結果爲(其中有挺多調試信息的):

點數爲1的概率爲0.167, count=1
點數爲2的概率爲0.167, count=1
點數爲3的概率爲0.167, count=1
點數爲4的概率爲0.167, count=1
點數爲5的概率爲0.167, count=1
點數爲6的概率爲0.167, count=1
sum=6, totalCount=6

點數爲2的概率爲0.028, count=1
點數爲3的概率爲0.056, count=2
點數爲4的概率爲0.083, count=3
點數爲5的概率爲0.111, count=4
點數爲6的概率爲0.139, count=5
點數爲7的概率爲0.167, count=6
點數爲8的概率爲0.139, count=5
點數爲9的概率爲0.111, count=4
點數爲10的概率爲0.083, count=3
點數爲11的概率爲0.056, count=2
點數爲12的概率爲0.028, count=1
sum=36, totalCount=36

點數爲3的概率爲0.005, count=1
點數爲4的概率爲0.014, count=3
點數爲5的概率爲0.028, count=6
點數爲6的概率爲0.046, count=10
點數爲7的概率爲0.069, count=15
點數爲8的概率爲0.097, count=21
點數爲9的概率爲0.116, count=25
點數爲10的概率爲0.125, count=27
點數爲11的概率爲0.125, count=27
點數爲12的概率爲0.116, count=25
點數爲13的概率爲0.097, count=21
點數爲14的概率爲0.069, count=15
點數爲15的概率爲0.046, count=10
點數爲16的概率爲0.028, count=6
點數爲17的概率爲0.014, count=3
點數爲18的概率爲0.005, count=1
sum=216, totalCount=216

迭代解法

如果理解了上面的遞歸解法,其實轉成迭代也很簡單!

假設我們知道了前n-1個骰子構成的點數的情況,比如點數爲2有1中情況,點數爲3有3種情況……,那麼就可以很輕鬆推斷出前n個骰子構成的點數的情況了!就是分別給前面的每個點數加上1,2,3,4,5,6,構成新的點數的情況數。

原書用了兩個數組,我用了兩個map,空間的重用性不夠好,不過可讀性增加了一點:

#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
using namespace std;


void cal(int n) {
    if (n <= 0)
        return;
    static const int low = 1, high = 6;
    map<int, int> count;
    count[0] = 1;
    for (int i = 1; i <= n; ++i) {
        map<int, int> now;
        map<int, int>::iterator it = count.begin();
        while (it != count.end()) {
            int number = it->first, cnt = it->second;
            for (int j = low; j <= high; ++j) {
                now[number + j] += cnt;
            }
            ++it;
        }
        count = now;
    }

    int totalCount = pow(6, n), sum = 0;
    map<int, int>::iterator it = count.begin();
    while (it != count.end()) {
        sum += it->second;
        printf("點數爲%d的概率爲%.3lf, count=%d\n", 
        it->first, it->second*1.0/totalCount, it->second);
        ++it;
    }
    printf("sum=%d, totalCount=%d\n\n", sum, totalCount);
}


int main() {
    cal(1);
    cal(2);
    cal(3);

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章