TCO17 Austin RainbowSocks

public class RainbowSocks
{
    public double getPairProb(int[] socks, int colordiff)
    {
        int len = socks.length;
        int total = len * (len - 1) / 2;
        int cnt = 0;
        for (int i = 0; i < len; i++)
        {
            for (int j = i + 1; j < len; j++)
            {
                if (Math.abs(socks[i] - socks[j]) <= colordiff)
                {
                    cnt++;
                }
            }
        }

        return (double)cnt/ total;
    }
}

你可能感兴趣的:(#,topcoder)