Skip to content

Commit c2e7f68

Browse files
Merge pull request matthewsamuel95#265 from KevinLu/master
Added power function implemented in Java
2 parents 92cec7c + a042c3a commit c2e7f68

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Scanner;
2+
3+
public class MaximumSumIncreasingSubsequence {
4+
public static int MaximumSumIncreasingSubsequence(int[] A, int n) {
5+
int max = 0;
6+
int dp[] = new int[n];
7+
8+
for (int i = 0; i < n; i++) {
9+
dp[i] = A[i];
10+
}
11+
12+
for (int i = 1; i < n; i++) {
13+
for (int j = 0; j < i; j++) {
14+
if (dp[i] < dp[j] + A[i] && A[i] > A[j]) {
15+
dp[i] = dp[j] + A[i];
16+
}
17+
}
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
if (dp[i] > max) {
22+
max = dp[i];
23+
}
24+
}
25+
26+
return max;
27+
}
28+
29+
public static void main(String[] args) {
30+
//Dynamic Programming approach
31+
Scanner sc = new Scanner(System.in);
32+
33+
int n = sc.nextInt();
34+
int[] A = new int[n];
35+
36+
for (int i = 0; i < n; i++) {
37+
A[i] = sc.nextInt();
38+
}
39+
40+
System.out.println(MaximumSumIncreasingSubsequence(A, n));
41+
}
42+
}

Math/Power/Power.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Scanner;
2+
3+
public class Power {
4+
public static double power (double x, int n) {
5+
if (n == 0) {
6+
return 1.00;
7+
}
8+
if (n == 1) {
9+
return x;
10+
}
11+
double ans = power(x, n / 2);
12+
13+
if (n % 2 == 0) {
14+
return ans * ans;
15+
} else if (n < 0) {
16+
return ans * ans / x;
17+
} else {
18+
return ans * ans * x;
19+
}
20+
}
21+
public static void main(String[] args) {
22+
Scanner sc = new Scanner(System.in);
23+
24+
double n = sc.nextDouble(); int p = sc.nextInt();
25+
double ans = power(n, p);
26+
27+
System.out.println(ans);
28+
}
29+
}

0 commit comments

Comments
 (0)