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

前端 未结 30 2830
清酒与你
清酒与你 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:40

    Or, you could abuse the preprocessor:

    #define f(n) (f##n)
    #define ff(n) -n
    
    int main()
    {
      int n = -42;
      cout << "f(f(" << n << ")) = " << f(f(n)) << endl;
    }
    

提交回复
热议问题