UVA-136Ugly numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500’th ugly number. Input There is no input to this program. Output Output should consist of a single line as shown below, with ‘’ replaced by the number computed. Sample Output The 1500'th ugly number is .
#include<bits/stdc++.h>
int b[3]={2,3,5};
using namespace std;
int main()
{
set<long long>s;
priority_queue<long long,vector<long long>,greater<long long> >que;
que.push(1);
s.insert(1);
int k;
for(k=1;;k++)
{ if(k==1500){ printf("The 1500'th ugly number is %lld.\n",que.top());return 0; }
long long cnt=que.top();
que.pop();
for(int i=0;i<=2;i++)
{
long long z=cnt*(long long) b[i];
if(!s.count(z)){ s.insert(z); que.push(z); }
}
}
return 0;
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步