Sword refers to offer 34_ ugly number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.