Last active
August 29, 2015 14:06
-
-
Save 910JQK/f3e5852ac043f0a2f9ec to your computer and use it in GitHub Desktop.
[140913]Power
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
typedef unsigned int uint; | |
using std::cin; | |
using std::cout; | |
uint power(uint a, uint n){ | |
if(a == 1) | |
return 1; | |
if(a != 0 && n == 0) | |
return 1; | |
if(a == 0 && n == 0) | |
throw "invalid argument"; | |
uint t, r; | |
t = a; | |
r = 1; | |
while(n > 0){ | |
if(n % 2 == 1) | |
r *= t; | |
n /= 2; | |
t *= t; | |
} | |
return r; | |
} | |
int main(int argc, char *argv[]){ | |
uint a, n; | |
cin >> a >> n; | |
cout << power(a, n) << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment