-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathugly_number.cpp
More file actions
executable file
·54 lines (48 loc) · 1.42 KB
/
ugly_number.cpp
File metadata and controls
executable file
·54 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
*
*题目描述
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
*
*/
#include <iostream>
using namespace std;
int Min(int a, int b, int c)
{
int temp = (a < b ? a : b);
return (temp < c ? temp : c);
}
int FindUgly(int n) //
{
int* ugly = new int[n];
ugly[0] = 1;
int index2 = 0;
int index3 = 0;
int index5 = 0;
int index = 1;
while (index < n)
{
int val = Min(ugly[index2]*2, ugly[index3]*3, ugly[index5]*5); //竞争产生下一个丑数
if (val == ugly[index2]*2) //将产生这个丑数的index*向后挪一位;
++index2;
if (val == ugly[index3]*3) //这里不能用elseif,因为可能有两个最小值,这时都要挪动;
++index3;
if (val == ugly[index5]*5)
++index5;
ugly[index++] = val;
}
/*
for (int i = 0; i < n; ++i)
cout << ugly[i] << endl;
*/
int result = ugly[n-1];
delete[] ugly;
return result;
}
int main()
{
int num;
cout << "input the number : " ;
cin >> num;
cout << FindUgly(num) << endl;
return 0;
}