Skip to content

Instantly share code, notes, and snippets.

@bojieli
Created May 10, 2015 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojieli/0153c05de25af8f9d0a5 to your computer and use it in GitHub Desktop.
Save bojieli/0153c05de25af8f9d0a5 to your computer and use it in GitHub Desktop.
Exercise 1-1
using namespace std;
#include <iostream>
int getPower(int x, int n) {
if (x == 0)
return 0;
if (n == 0)
return 1;
else if (n > 0)
return getPower(x, n-1) * x;
else // n < 0
return getPower(x, n+1) / x;
}
double getPower(double x, int n) {
if (n == 0)
return 1;
else if (n > 0)
return getPower(x, n-1) * x;
else // n < 0
return getPower(x, n+1) / x;
}
int main() {
int a;
double b;
int m;
cin >> a >> b >> m;
cout << getPower(a,m) << endl << getPower(b,m) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment