数组中查找最大值
Given an array and we have to find maximum absolute difference.
给定一个数组,我们必须找到最大的绝对差。
Approach to follow:
遵循的方法:
In the first step, we take input an array with few elements.
第一步,我们输入一个包含很少元素的数组。
int[] array = {10,20,50,80,90};
In the second step, we will find the maximum and minimum element of an array.
在第二步中,我们将找到数组的最大和最小元素。
In the third step, we will subtract the minimum element from the maximum element of an array so the difference between the minimum and maximum element of an array is the maximum absolute difference of an array.
在第三步中,我们将从数组的最大元素中减去最小元素,以便数组的最小元素和最大元素之间的差是数组的最大绝对差。
Example:
例:
// Java program to find the maximum absolute difference
// of an array
class MaximumAbsoluteDifferenceOfArray {
public static void main(String[] args) {
// Declare and initialize an array
int[] array = {
10,
20,
50,
80
};
int num_of_elements = array.length;
// To store the minimum and the maximum elements
// from the array and assigning first element
int min = array[0];
int max = array[0];
for (int i = 1; i < num_of_elements; i++) {
// We are comparing first element with all other elements
min = Math.min(min, array[i]);
max = Math.max(max, array[i]);
}
int abs_diff = max - min;
System.out.println("The maximum absolute difference of an array is " + abs_diff);
}
}
Output
输出量
E:\Programs>javac MaximumAbsoluteDifferenceOfArray.java
E:\Programs>java MaximumAbsoluteDifferenceOfArray
The maximum absolute difference of an array is 70
翻译自: https://www.includehelp.com/java/find-maximum-absolute-difference-in-an-array.aspx
数组中查找最大值