能帮我看下哪块错了么
查看原帖
能帮我看下哪块错了么
326739
Faith020楼主2020/10/21 19:22
#include<iostream>
#include <climits>
#include <algorithm>
#include <set>

using namespace std;
set<long> s;

void push(long l) {
    set<long>::iterator it = s.begin();
    while (it != s.end()) {
        if (*it == l) {
            cout << "Already Exist" << endl;
            return;
        } else
            break;
    }
    s.insert(l);
}

void pop(int l) {
    if (s.empty()) {
        cout << "Empty" << endl;
    }else{
        set<long>::iterator cur;
        set<long>::iterator it = s.begin();
        long min = LONG_MAX;
        while (it != s.end()){
            if (abs(*it - l) < min)
            {
                cur = it;
                min = abs(*it - l);
            }
            it++;
        }
        
        cout << *cur << endl;
        s.erase(cur);
    }

}

int main() {
    int n;
    cin >> n;
    long a, l;
    while (n--) {
        cin >> a >> l;
        if (a == 1)
            push(l);
        else
            pop(l);
    }
    return 0;
}


2020/10/21 19:22
加载中...