【C++编程基础】AOJ (C++ Programming II)—3-B-min-max element

3-B-min-max element

题目

Write a program which manipulates a sequence A={a0,a1,…,an−1} by the following operations: min(b,e): report the minimum element in ab,ab+1,…,ae−1 max(b,e): report the maximum element in ab,ab+1,…,ae−1
输入
The input is given in the following format. n a0 a1,…,an−1 q com1 b1 e1 com2 b2 e2 : comq bq eq In the first line, n (the number of elements in A) is given. In the second line, ai (each element in A) are given. In the third line, the number of queries q is given and each query is given in the following q lines. comi denotes a type of query. 0 and 1 represents min(b,e) and max(b,e) respectively
输出
For each query, print the minimum element or the maximum element in a line.
样例输入
7
8 3 7 1 9 1 4
3
0 0 3
0 1 5
1 0 7
样例输出
3
1
9

题解(代码流程)

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, q;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++)cin >> A[i];
    cin >> q;
    while (q--) {
        int op, left, right;
        cin >> op >> left >> right;
        if (op == 0) {
            cout << *min_element(A.begin() + left, A.begin() + right) << endl;
        } else if (op == 1) {
            cout << *max_element(A.begin() + left, A.begin() + right) << endl;
        }
    }
    return 0;
}

小结

运用min_element(first,end)
max_element(first,end) 返回容器中最大小值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值