Recursive power function: approach

后端 未结 9 1157
北恋
北恋 2020-12-18 15:24

I\'m programming for a while now(beginner), and recursive functions are a somewhat abstract concept for me. I would not say I\'m stuck, program works fine, I\'m just wonderi

9条回答
  •  北海茫月
    2020-12-18 15:39

    Simple but does n number of multiplications. Above examples are more efficient because they group two operations in one iteration

    int power(int x, int n)
    {
        if (n == 0) return 1;
    
        return x * power(x, n-1);
    }
    

提交回复
热议问题