C++大法好 优先队列 牛批 求第1500个丑数是什么 priority_queue既然有自动排序的功能 set 有去重的功能 牛批 不过还是太慢了 哈哈 毕竟还是要一直的查重和排序 还是太慢了
#include <iostream>
#include <vector>
#include <queue>
#include <set>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
typedef long long LL;
const int coeff[3] = {2,3,5};
int main(void)
{
priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
pq.push(1);
s.insert(1);
for(int i = 1;;i++)
{
LL x = pq.top();pq.pop();
if(i == 1500)
{
cout << x;
break;
}
for(int j = 0;j < 3;j++)
{
LL x2 = x*coeff[j];
if(!s.count(x2)){s.insert(x2);pq.push(x2);}
}
}
return 0;
}
模板虽然用着舒服 至少可以不用自己再造数据结构 但是感觉还是少用为妙 因为自己不懂造的轮子 再怎么用有可能就锻炼不到思维了还是数组大法好 效率杠杠的
往往牛批的思想不会用到高端的数据结构 我是这么感觉的
#include<stdio.h>
#define N 1500
int s[N]={1};
int min(int a, int b, int c)
{
if(a<b)return a<c?a:c;
else return b<c?b:c;
}
void init()
{
int f2=2,f3=3,f5=5,i2=0,i3=0,i5=0;
int i=1;
while(i<N)
{
s[i] = min(f2,f3,f5);
if(s[i]==f2)
f2 = s[++i2]*2;
if(s[i]==f3)
f3 = s[++i3]*3;
if(s[i]==f5)
f5=s[++i5]*5;
i++;
}
}
int main()
{
init();
printf("The 1500'th ugly number is %lld.\n",s[1500-1]);
return 0;
}