forked from Sandip-Jana/Algorithms-ACM-ICPC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPower.java
More file actions
29 lines (25 loc) · 667 Bytes
/
Power.java
File metadata and controls
29 lines (25 loc) · 667 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
import java.util.Scanner;
public class Power {
public static double power (double x, int n) {
if (n == 0) {
return 1.00;
}
if (n == 1) {
return x;
}
double ans = power(x, n / 2);
if (n % 2 == 0) {
return ans * ans;
} else if (n < 0) {
return ans * ans / x;
} else {
return ans * ans * x;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double n = sc.nextDouble(); int p = sc.nextInt();
double ans = power(n, p);
System.out.println(ans);
}
}