commit
53ee73fe09
32
Maths/AbsoluteMax.java
Normal file
32
Maths/AbsoluteMax.java
Normal file
@ -0,0 +1,32 @@
|
||||
package Maths;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* description:
|
||||
* <p>
|
||||
* absMax([0, 5, 1, 11]) = 11, absMax([3 , -10, -2]) = -10
|
||||
* </p>
|
||||
*/
|
||||
public class AbsoluteMax {
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[]{3, -10, -2};
|
||||
System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
|
||||
}
|
||||
|
||||
/**
|
||||
* get the value, it's absolute value is max
|
||||
*
|
||||
* @param numbers contains elements
|
||||
* @return the absolute max value
|
||||
*/
|
||||
public static int absMax(int[] numbers) {
|
||||
int absMaxValue = numbers[0];
|
||||
for (int i = 1, length = numbers.length; i < length; ++i) {
|
||||
if (Math.abs(numbers[i]) > Math.abs(absMaxValue)) {
|
||||
absMaxValue = numbers[i];
|
||||
}
|
||||
}
|
||||
return absMaxValue;
|
||||
}
|
||||
}
|
32
Maths/AbsoluteMin.java
Normal file
32
Maths/AbsoluteMin.java
Normal file
@ -0,0 +1,32 @@
|
||||
package Maths;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* description:
|
||||
* <p>
|
||||
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
|
||||
* </p>
|
||||
*/
|
||||
public class AbsoluteMin {
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[]{3, -10, -2};
|
||||
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
|
||||
}
|
||||
|
||||
/**
|
||||
* get the value, it's absolute value is min
|
||||
*
|
||||
* @param numbers contains elements
|
||||
* @return the absolute min value
|
||||
*/
|
||||
public static int absMin(int[] numbers) {
|
||||
int absMinValue = numbers[0];
|
||||
for (int i = 1, length = numbers.length; i < length; ++i) {
|
||||
if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
|
||||
absMinValue = numbers[i];
|
||||
}
|
||||
}
|
||||
return absMinValue;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user