警钟长鸣(后五个点WA)
查看原帖
警钟长鸣(后五个点WA)
1436960
Arabidopsis楼主2024/11/10 08:32

在你的CompareTo(也就是cmp里,一定不要偷懒不处理相等的情况,或者是把等于默认变成了小于),i.e.

class Point : IComparable<Point>
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }

    public int CompareTo(Point other)
    {
        if (Z > other.Z) return 1;
        else return -1;
    }

    public double GetDistance(Point other)
    {
        return Math.Sqrt(Math.Pow((X-other.X), 2)+Math.Pow((Y-other.Y), 2)+Math.Pow((Z-other.Z), 2));
    }
}

CompareTo应该是:

public int CompareTo(Point other)
{
    if (Z >= other.Z) return 1;
    else return -1;
}

或者在等于的时候单独返回一个0。

2024/11/10 08:32
加载中...