Ugly Numbers
Descriptions:
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 <number>.
题意
丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:
1,2,3,4,5,6,8,9,10,12,15……
求第1500个丑数
输入
没有输入
输出
The 1500'th ugly number is <number>.
题目链接:
https://vjudge.net/problem/UVA-136
不难发现2,3,5之后的丑数全是有这三个数乘上{2,3,5}之后得来的,这就好办了,因为不可重复,还需要按顺序来,所以选择<set>是没有问题的,用<set>存入这些丑数,记录 it 的大小,当it为1500-1(数组从0开始计数)时就是我们要找的这个丑数
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
set<ll> num;//存入丑数
set<ll>::iterator it;
int main()
{
int a[]= {,,};
num.insert();
int sum=;//这里面肯定会有重复的,所以要重新设一个计数器
it=num.begin();//从0开始计数
while(sum<-)//当然sum的值可以更大一点,这也没事,主要是去重复
{
for(int i=; i<; i++)
num.insert(*it * a[i]);
it++;//记录当前为第几个丑数
sum++;
}
printf("The 1500'th ugly number is %llu.\n", *it);
}