确实没学过C++,STL也是找猫画虎的用: 但还是想知道为什么自己定义的cmp不行。。
#include<iostream>
#include<queue>
#include<set> //去除重复性
#include<vector>
using namespace std;
typedef long long LL;
struct cmp{
bool operator ()(const LL a,const LL b) { return a<b; }
};
const int coeff[]={2,3,5};
/* 这里改成greater<LL>就可以了,但想知道自定义结构体cmp重载"()"为啥不对 */
priority_queue< LL,vector<LL>,cmp > pq;
set<LL> s;
int main(){
pq.push(1);
s.insert(1);
for(int i=1;;++i){
LL x=pq.top(); //if(i<100) printf("[%d]",x);
pq.pop();
if(i==1500){
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for(int j=0;j<3;++j){
LL n=coeff[j]*x;
if(s.count(n)==0){
pq.push(n);
s.insert(n); //if(i<100) printf("[Insert %d]",n);
}
} //if(i<100)puts("");
}
return 0;
}