AcWing one question every day during winter vacation-the biggest fluctuation on Day38

Xiao Ming is using the volatility of stocks to study stocks.

Xiao Ming got the daily closing price of a stock. He wanted to know the maximum fluctuation value of this stock for several consecutive days, that is, the absolute value of the difference between the closing price of a certain day and the closing price of the previous day during these few days. how many.

Input format
The first line of input contains an integer n, which represents the number of consecutive days for the closing price that Xiao Ming received.

The second line contains n positive integers, which in turn represent the closing price of each day.

Output format
Output an integer, which represents the maximum fluctuation value of this stock in n days.

Data range
For all evaluation use cases, 2 ≤ n ≤ 1000. 2≤n≤1000.2n1 0 0 0 .
The daily price of the stock is an integer between 1 and 10,000.

Input sample:

6
2 5 5 7 3 5

Sample output:

4

Sample explanation
The fluctuation between the fourth day and the fifth day is the largest, and the fluctuation value is |3−7|=4.

Analysis: Simulation

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,a[N],m;
int main(){
    
    
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n;i++) m=max(m,abs(a[i]-a[i-1]));
    cout<<m<<endl;
}

Guess you like

Origin blog.csdn.net/messywind/article/details/113889089