面试题 43 n 个骰子的点数

int g_maxValue = 6;

void printfProbability(int number)

{    



    if(number < 1) return;

    

    int * p[2];

    p[0] = new int[number * g_maxValue + 1];

    p[1] = new int[number * g_maxValue + 1];



    memset(p[0], 0, sizeof(int) *(number * g_maxValue + 1));

    memset(p[1], 0, sizeof(int) *(number * g_maxValue + 1));



    int flag = 0;

    

    for(int i = 1; i<= g_maxValue ; ++i)

        p[flag][i] = 1;

    

    for(int k = 2; k <= g_maxValue; ++k){

    

        for(int j = 0; j< k; ++j)

            p[1-falg][j] = 0;

            

        for(int i = k; i <= g_maxValue * k ;++i){

            p[1-flag][i] = 0

            for(int j = 1; j <= i && j <= g_maxValue ;++j)

                {

                    p[1-flag][i] += p[flag][i-j];

                }

    

        }

        flag = 1-flag;

    }

    

    double total = pow((double)g_maxValue, number);

    for(int i = number; i<= number *g_maxValue ;++i)

    {

        double ratio = (double)p[k][i]/total;

        printf("%d : %e\n", i, ratio);

    }

    

    delete [] p[0];

    delete [] p[1];

}

 

类似青蛙跳台阶的问题

 

 

你可能感兴趣的:(面试题)