Recursive power function: approach

后端 未结 9 1186
北恋
北恋 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 16:03

    int pow(int a, int n) {
        if(n == 0) return 1;
        if(n == 1) return a;
        int x = pow(a, n/2);
        if(n%2 == 0) {
            return x*x;
        }
        else {
            return a*x*x;
        }
    }
    

提交回复
热议问题