Recursive power function: approach

后端 未结 9 1173
北恋
北恋 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:47

    My approach with C++, works only with non-negative numbers.

    #include 
    using namespace std;
    
    long long  power(long long x, long long y) {
    if (y == 0) {
        return  1;
    }
    else {
        y--;
        return x*power(x,y);
     }
    }
    main() {
    long long n, x;
    cin >> n >> x;
    cout << power(n,x);
    }
    

提交回复
热议问题