10_2足球比赛问题

2017-09-28  本文已影响34人  X_Y

有2k只球队,有k-1个强队,其余都是弱队,随机把它们分成k组比赛,每组两个队,问两强相遇的概率是多大?

给定一个数k,请返回一个数组,其中有两个元素,分别为最终结果的分子和分母,请化成最简分数

测试样例:
输入:4
返回:[3,7]

class Championship {
public:
    int factorial(int n)
    {
        if(n == 0) return 1;
        return n*factorial(n-1);
    }

    int permutation(int i, int j)
    {
        return factorial(i) / factorial(i - j);
    }

    int combination(int i, int j)
    {
        return permutation(i, j) / factorial(j);
    }

    int gcd(int a, int b)
    {
        if(0 == a % b) return b;
        else{
            return gcd(b, a % b);
        }
    }

    vector<int> calc(int k) {
        // write code here
        vector<int> res(2,0);
        if(k-1<2){
            res[0] = 0;
            res[1] = 1;
            return res;
        }
        int a = 1, b = 0;
        for(int i=0; i<k; ++i){
            a *= (i*2 + 1);
        }
        b = combination(k+1, 2) * factorial(k-1);
        int grt_cmn_dvsr = gcd(a, a-b);
        res[0] = (a-b) / grt_cmn_dvsr;
        res[1] = a / grt_cmn_dvsr;
        return res;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读