Luogu brush questions C++ language | P5724 Seeking extreme difference

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Given  n  and  n  integers  ai , find   the range among these n integers. Range means the difference between the maximum value minus the minimum value in a set of numbers.

【enter】

Input a positive integer  n in the first line , indicating the number of integers.

The second line inputs  n  integers  a 1, a 2… an separated by spaces.

【Output】

Output an integer representing the range of these  n  integers.

【Input sample】

6 4 1 5 1 4 1

【Example of output】

4

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, a, max, min;
    cin >> n;
    cin >> a;
    max = a;
    min = a;
    for (int i=2; i<=n; i++) {
        cin >> a;
        if (max<a) max = a;
        if (min>a) min = a;
    }
    cout << max - min;
    return 0;
}

【operation result】

6
4 1 5 1 4 1
4

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132635075