Designing function f(f(n)) == -n

前端 未结 30 2838
清酒与你
清酒与你 2020-12-02 03:32

A question I got on my last interview:

Design a function f, such that:

f(f(n)) == -n

Where n<

30条回答
  •  忘掉有多难
    2020-12-02 03:56

    Works except int.MaxValue and int.MinValue

        public static int f(int x)
        {
    
            if (x == 0) return 0;
    
            if ((x % 2) != 0)
                return x * -1 + (-1 *x) / (Math.Abs(x));
            else
                return x - x / (Math.Abs(x));
        }
    

    pictorial

提交回复
热议问题