Cod sursa(job #2181504)

Utilizator gfx96Vasilache Andrei gfx96 Data 21 martie 2018 18:32:54
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <iostream>
#include <fstream>

using namespace std;

int exponent(int x,int n)
{
    if(n<0) return exponent(1/x,n*(-1));
    else if(n==0) return 1;
    else if(n==1) return x;
    else if(n%2==0) return exponent(x*x,n/2);
    else if(n%2!=0) return x*exponent(x*x,(n-1)/2);
}


int main()
{
    int x,n;
    ifstream f("lgput.in");
    ofstream o("lgput.out");
    f>>x>>n;

    exponent(x,n);
    o<<exponent(x,n);

    return 0;
}