vector和set

1.小欧的选数乘积

https://www.nowcoder.com/practice/a94f523ebe424d0481533dc9e6138724?tpId=376&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E9%259D%25A2%25E8%25AF%2595%26topicId%3D376

vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    long long x, y;
    cin >> x >> y;
    int n;
    cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    auto b = unique(a.begin(), a.end());
    a.erase(b, a.end());
   
    int res = 0;
    for (int i = a.size() - 1; i >= 0; i--){
        if (x < y){
            x = x * a[i];
            res++;
        } else {
            break;
        }
    }
    if (x  < y) {
        res = -1;
    }
    cout << res << endl;
}
// 64 位输出请用 printf("%lld")

 

set

#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main() {
    long long x, y;
    cin >> x >> y;
    int n;
    cin >> n;
    set<long long> a;
    long long b;
    for (int i = 0; i < n; i++) {
        cin >> b;
        a.insert(-b);
    }
    
   
    int res = 0;
    for (auto i : a){
        if (x < y){
            x = x * (-i);
            res++;
        } else {
            break;
        }
    }
    if (x  < y) {
        res = -1;
    }
    cout << res << endl;
}
// 64 位输出请用 printf("%lld")

 

posted @ 2025-06-18 09:20  最近饭吃的很多  阅读(4)  评论(0)    收藏  举报