首页 > 试题广场 >

#includeusing&nb...

[单选题]
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
struct stsort{
bool operator () (const int a, const int b) const{
if(gcd(30, a) < gcd(30, b)){
return 1;
}
else if(gcd(30, a) == gcd(30, b)){
return a < b;
}
else return 0;
}
};
int main(){
int n = 5;
priority_queue<int, vector<int>, stsort>q;
for(int i = 1; i <= n; ++i){
q.push(i);
}
for(int i = 1; i <= n; ++i){
printf("%d", q.top());
q.pop();
}
return 0;
}
程序的输出为( )
  • 53421
  • 53241
  • 12435
  • 14235
难,哈哈
发表于 2019-09-14 12:16:15 回复(0)
优先队列比较的方式使用 stsort重载,队列的输入是1,2,3,4,5  都会调用gcd函数算相对优先度,结果分别是1,2,3,2,5, 相同优先度的输出最大的一个,最终结果时5,3,4,2,1
发表于 2019-09-17 11:14:44 回复(0)