-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem79.cpp
More file actions
44 lines (36 loc) · 785 Bytes
/
problem79.cpp
File metadata and controls
44 lines (36 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
class PowerSpell {
double x; // base
int n; // exponent
public:
void readInput() {
cin >> x >> n;
if (x <= -100.0 || x >= 100.0) {
cout << "!! Invalid Base !!" << endl;
exit(1);
}
// Note: n can be any int in valid int range
}
double power(double base, int exp) {
if (exp == 0) return 1.0;
if (exp < 0) {
// Handle negative exponent
return 1.0 / power(base, -exp);
}
double half = power(base, exp / 2);
if (exp % 2 == 0)
return half * half;
else
return half * half * base;
}
void display() {
cout << fixed << setprecision(6) << power(x, n) << endl;
}
};
int main() {
PowerSpell ps;
ps.readInput();
ps.display();
return 0;
}