Problem Description:
We call the number that contains only the factor 2,3,5 as the ugly number, and we find the 1500th ugly number in the order from small to large. For example, 6,8 are all ugly numbers, but 14 is not because 14 contains factor 7. We are accustomed to 1 as the first ugly number. Problem Analysis:
Method 1: Starting from 1, each number is judged to see if it contains only the 2,3,5 factor until the 1500th ugly number is found. This method is easier to understand, but it has done a lot of work, for example, for the number of not-ugly, all of the mold in addition to the operation, so, time is relatively wasteful.
Method 2: In view of the above methods need to spend a lot of time in the number of not ugly, this approach using space for time, any one ugly is from the front of a smaller than his ugly number calculated, that is, the ugly number is certainly from the ugly number.
How to: We create an array of 1500 that stores the ugly numbers that are already sorted. Each of the ugly numbers inside is multiplied by an ugly number 2 or 3, or the last number in the current array must be the largest number of ugly in the array. We define 3 pointers, initialize the first element of the array, multiply the corresponding number by 2, multiply by 3, multiply by 5, get 3, and take out the smallest of 3, which must be the next ugly number and stored in the array. Each time you calculate an ugly number from the front number (three pointers to the number multiplied by 2,3,5), and if less than the last ugly number in the array, the corresponding pointer moves backwards. Find the 1500th ugly number and then output it. This is the general idea.
Method 3: Use the associated container set. Because the set stores a key that is not duplicated and is sorted in ascending order, you can use this feature to find the number of fools. For the set is not very clear reader, poke in http://blog.csdn.net/peiyao456/article/details/53161390 code implementation:
Method 1 This method is easy to implement, and in the interview generally will not be liked by the interviewer, so here only give the implementation of the method in the latter 2.
Method 2:
#include <iostream>
using namespace std;
int min (int x,int y,int z)
{
int min = (x < y)? x:y;
return min < Z? min:z;
}
int getuglynumber (int index)
{
if (index <= 0) return
0;
int* puglynumbers = new Int[index];
Puglynumbers[0] = 1;
int nextuglynumbers = 1;
int* pUgly2 = puglynumbers;
int* pUgly3 = puglynumbers;
int* pUgly5 = puglynumbers;
while (Nextuglynumbers < index)
{
int min = min (*pugly2*2,*pugly3*3,*pugly5*5);
puglynumbers[nextuglynumbers++] = min;
while (*pugly2 * 2 <= min)
pugly2++;
while (*pugly3 * 3 <= min)
pugly3++;
while (*PUGLY5 * 5 <= min)
pugly5++;
}
int ugly = puglynumbers[index-1];
Delete[] Puglynumbers;
return ugly;
}
int main ()
{
int index = 0;
cin>>index;
Cout<<getuglynumber (index) <<endl;
System ("pause");
return 0;
}
Method 3:
#include <iostream>
using namespace std;
#include <limits.h>
#include <set>
int getuglynumber (int index)
{
if (index <= 0)
return 0;
set<int,less<int>> s;
Set<int,less<int>>::iterator it;
S.insert (1);
for (int i = 1; i < index * 6/*int_max/5*/; ++i)
{
S.insert (i * 2);
S.insert (i * 3);
S.insert (i * 5);
}
int i = 0;
for (it = S.begin (), i = 1; it!= s.end () &&i < index; ++it,++i)
{}
cout<<*it<<endl;
}
int main ()
{
int index = 0;
cin>>index;
Getuglynumber (index);
System ("pause");
return 0;
}
In Method 3, the data is inserted into the set, and the range of I is exactly how much. To refer to http://blog.csdn.net/coder_xia/article/details/6707600 he gives the INT_MAX/5 because it prevents the ugliness from exceeding the maximum number of integers. I follow this method to try, the program runs for a long time (specific how long I did not test), I gave is index*6, that is, the need for the number of ugly sequence 6 times times, why I want to multiply a number. (In fact, you can also correctly find the 1500th ugly number) but still afraid of too much duplication of data (15 = 3 * 5, is the ugly number, i = = 3 o'clock, will calculate the 15,i==5, it will be calculated 15), so here multiply a factor.
There are several ways to do this topic, the author of the above link is really serious ~ ~ interested in the can go to see for themselves.