STL 集合(set)

其他的不说,主要提供两份代码,包含了常用到的集合操作:

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

typedef __int64 LL;

LL x, y, k;

set<LL> v;

LL GCD(LL x, LL y){
    if(y == 0)
        return x;
    else
        return GCD(y,x%y);
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        v.clear();
        scanf("%I64d %I64d %I64d", &x, &y, &k);
        LL gTmp = GCD(x, y);
        for(LL i=1;i*i<=gTmp;i++){
            if(gTmp%i==0){
                v.insert(i);
                v.insert(gTmp/i);
            }
        }

        if(k<=v.size()){
            int time=0;
            for(set<LL>::iterator it=v.begin();it!=v.end();it++){
                if(time==v.size()-k){
                    cout<<*it<<endl;
                    break;
                }
                time++;
            }
        }
        else
            printf("-1\n");
    }

    return 0;
}

判断集合是否相等:

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

typedef __int64 ll;
set<ll> st1, st2;

int main(){
    int n, cn;
    bool flag;
    ll a;
    while(cin>>n){
        st1.clear();
        st2.clear();
        for(int i=0;i<n;i++){
            cin>>a;
            st1.insert(a);
        }
        for(int i=0;i<n;i++){
            cin>>a;
            st2.insert(a);
        }
        if(st1==st2) printf("YES\n");
        else printf("NO\n");
        /*
        flag = true;
        if(st1.size() != st2.size()) flag = false;
        if(flag){
            for(set<ll>::iterator it1=st1.begin(), it2=st2.begin();it1!=st1.end();it1++, it2++){
                if(*it1 != *it2){
                    flag = false;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        */

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值