求一组数据中所有以下条件的数arr={n1,n2,n3...}:
(1)这些数既大于所有它左边的数
(2)又大于所有它右边的数。
输出所有的满足上述条件得数,如果没有就什么都不输出.
思路:
一般的思路是
(1)对于这样一组数据{ 2, 3, 1, 2, 8, 9, 11, 22, 13 ,77,888,999}
(2)以数组中每个数据做为基准点,去它检查左边的数据是否都小于它,右边的数据是否都大于他.
(3)如果都满足,则输出该数据。
(4)这样当这组数据都遍历完一遍的时候,所有满足上述条件的数,也就找到了。
(5)有两种情况是肯定没有这个数的:1.数组中元素数目小于三个 2.数组中的第一个数和最后一个数
代码实现(Java):
public class SpecPoint { public static void main(String[] args) { // int[] arr = { 2, 3, 1, 2, 8, 9, 11, 22, 13, 77, 888, 999 }; int[] arr = { 10, 30, 1, 70, 90, 199, 100, 299, 399 }; // int[] arr = {10,11,19,88,}; // int[] arr = {}; // int[] arr = { 121,3,4,11,999,888,22,2222,77777,99999}; boolean greater_than_front; boolean smaller_than_back; ; for (int i = 1; i < arr.length - 1; i++) { greater_than_front = true; smaller_than_back = true; for (int j = 0; j < i; j++) { if (arr[i] <= arr[j]) { greater_than_front = false; break; } } if (!greater_than_front) continue; for (int j = i + 1; j < arr.length; j++) { if (arr[i] >= arr[j]) { smaller_than_back = false; break; } } if (!smaller_than_back) continue; if (greater_than_front && smaller_than_back) { System.out.println(arr[i]); } } } }