从Robert Sedgewick的算法中理解算法

时间:2014-05-19 19:13:46

标签: c# algorithm

研究罗伯特·塞奇威克的算法我发现他的例子不是为我清楚。

考虑以下算法:

    public static int count(int[] a, out int ifExecutions)
    {  // Count triples that sum to 0.
        ifExecutions = 0;
        int N = a.Length;
        int cnt = 0;
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
                for (int k = j + 1; k < N; k++)
                {
                    ifExecutions ++;
                    if (a[i] + a[j] + a[k] == 0) //   N (N - 1)(N - 2) / 6 
                        cnt++;
                }
        return cnt;
    }
Robert Sedgewick在他的书中写道:

  

....其他人需要更高级别的推理:例如,if语句   在ThreeSum.count()中精确执行N(N - 1)(N - 2)/ 6 ....

我无法弄清楚这里出现了数字“6”的方式。有人可以解释一下吗?

0 个答案:

没有答案
相关问题