day41 3232 最大波动 (枚举)

  1. 最大波动

小明正在利用股票的波动程度来研究股票。

小明拿到了一只股票每天收盘时的价格,他想知道,这只股票连续几天的最大波动值是多少,即在这几天中某天收盘价格与前一天收盘价格之差的绝对值最大是多少。

输入格式
输入的第一行包含了一个整数 n,表示小明拿到的收盘价格的连续天数。

第二行包含 n 个正整数,依次表示每天的收盘价格。

输出格式
输出一个整数,表示这只股票这 n 天中的最大波动值。

数据范围
对于所有评测用例, 2 ≤ n ≤ 1000 。 2≤n≤1000。 2n1000
股票每一天的价格为 110000 之间的整数。

输入样例:

6
2 5 5 7 3 5

输出样例:

4

样例解释
第四天和第五天之间的波动最大,波动值为 ∣ 3 − 7 ∣ = 4 。 |3−7|=4。 37=4

扫描二维码关注公众号,回复: 12626900 查看本文章

思路:

这就是一个简单的枚举题,即从头至尾枚举每相邻两天的差值,不断更新差值的最大值即可。

Java代码

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[]  arr = new int[n];
        for(int i = 0;i < n;i++){
    
    
            arr[i] = sc.nextInt();
        }
        int res = Integer.MIN_VALUE;
        for(int i = 1;i < n;i++){
    
    
            int temp = Math.abs(arr[i] - arr[i - 1]);
            res = Math.max(res,temp);
        }
        System.out.println(res);
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YouMing_Li/article/details/114042397