<span style="font-size:18px;">#include <iostream>
#include <queue>
#include <cstdio>
#include <set>
using namespace std;
long long record[3]={2,3,5};
priority_queue<long long,vector<long long>,greater<long long> > pq;
set<long long> s;
int main()
{
long long i,t,x,j;
pq.push(1);
s.insert(1);
for(i=1;;i++)
{
x=pq.top();
pq.pop();
if(i==1500)
{
printf("The 1500'th ugly number is %d.\n",x);
break;
}
for(j=0;j<3;j++)
{
t=x*record[j];
if(!s.count(t)){pq.push(t);s.insert(t);}
}
}
return 0;
}</span>
这道题,一开始以为自己都看懂了,就懒得自己写,只打算照着书上的代码打一遍。但是后来还是决定自己写下试试,写了才发现有个地方没有理解清楚。。。set里面的丑数没按顺序,所以不能在set的大小为1500的时候就输出,因为这时候可能还有更小的丑数还没存进来。所以以后就算是例题也要合上书自己写一遍。。。不然以为都懂了其实还并没懂。。 |