求一组数据中所有既大于所有它左边,又大于所有它右边的数?

求一组数据中所有以下条件的数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]);
            }

        }

    }

}

转载于:https://www.cnblogs.com/ChrisWang/articles/1600932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值