其他的不说,主要提供两份代码,包含了常用到的集合操作:
#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;
}