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 .
利用set会自动排序的性质来写。
注意输出格式错误❌,也可能报告为wrong answer。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;
int count=0;
int main()
{
set<long long>s;//定义一个longlong类型的
set<long long>::iterator it;//迭代器
s.insert(1);
for(it=s.begin();it!=s.end();it++)
{
count++;
if(count==1500)
{
printf("The 1500'th ugly number is %lld.\n",*it);
}
s.insert((*it)*2);
s.insert((*it)*3);
s.insert((*it)*5);
}
return 0;
}
该程序通过集合数据结构寻找第1500个丑数,丑数是仅包含质因数2、3和5的正整数。程序首先将1插入集合,然后迭代集合中的每个元素,将其乘以2、3和5并插入集合,直到找到第1500个丑数并输出。
506

被折叠的 条评论
为什么被折叠?



