丑数 UVa136

AC的C++程序如下:

#include<iostream>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
int yinzi[3]={2,3,5};
int main()
{
	priority_queue<ll,vector<ll>,greater<ll> >q;
	set<ll> s;
	int ans=0;
	q.push(1);
	s.insert(1);
	while(1)
	{
		ll n=q.top();
		q.pop();
		ans++;
		if(ans==1500)
		{
		 cout << "The 1500'th ugly number is " << n<<"."<<endl;
         break;
		}
		for(int i=0;i<3;i++)
		{
			ll m=n*yinzi[i];
			if(!s.count(m))
			{
				q.push(m);
				s.insert(m);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/87096767
今日推荐