Number of Ugly: only 2 3 5 The number of these three factors, before (section) 1500. We used to think of 1 as the first ugly number.
For example 6, 8 is the number of ugly. 14 not.
#include <stdio.h>intMin (intXintYintz) { intmin = (x <= y)?x:y; returnmin = (z <= min)?z:min;}intGetuglynumber (intindex) { if(Index <= 0){ return0; } int* uglynumbers=New int[index]; intUglycount = 1; uglynumbers[0] = 1; int* Ugly2 =uglynumbers; int* Ugly3 =uglynumbers; int* Ugly5 =uglynumbers; for(; uglycount< index; + +Uglycount) { intmin = min (*ugly2), (*ugly3), (*ugly5)); Uglynumbers[uglycount]=min; while((*ugly2) * * <=min) { ++Ugly2; } while((*ugly3) <=min) { ++Ugly3; } while((*ugly5) <=min) { ++ugly5; } } for(inti=0; i<index; i++) {printf ("%d\n", Uglynumbers[i]); } delete[] uglynumbers;}intMain () {intindex = 1500; Getuglynumber (index); return0;}
GCC UglyNumber.cpp
1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
25
27
30
32
36
.
.
.
786432000
787320000
791015625
796262400
797161500
800000000
805306368
806215680
810000000
816293376
819200000
820125000
829440000
838860800
839808000
843750000
849346560
850305600
854296875
859963392
The 1500th one is 859963392.
Sword refers to offer 34_ ugly number