【CCF】——Maximum fluctuation

Problem description
  Xiaoming 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 daily closing price.
Output format
  outputs an integer that represents the maximum fluctuation value of this stock in n days.
Sample input

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.
Evaluation use case scale and conventions
  For all evaluation use cases, 2 ≤ n ≤ 1000. The daily price of the stock is an integer between 1 and 10,000.

Take the absolute value of the next one minus the previous one, and compare it with maxn, which records the maximum fluctuation.
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    
    
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int a[n];
	for(int i = 0;i<n;i++){
    
    
		cin >> a[i];
	}
	int maxn = 0;
	for(int i = 1;i<n;i++){
    
    
		int x = abs(a[i]-a[i-1]);
		if(x>maxn)
			maxn = x;
	}
	cout << maxn;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108549062