The sword refers to the offer series---look for the ugly number

Source: Internet
Author: User

The "title" refers to numbers that contain only factors 2, 3, and 5 as ugly numbers (Ugly number).
* For example, 6, 8 are ugly numbers, but 14 is not because it contains factor 7. We used to think of 1 as the first ugly number. Find the nth ugly number in order from small to large.

Solution One: This solution commits a run timeout and is not recommended.

1  PackageCom.exe11.offer;2 3 /**4 * "title" refers to numbers that contain only factors 2, 3, and 5 (Ugly number). 5 * For example, 6, 8 are ugly numbers, but 14 is not because it contains factor 7. We used to think of 1 as the first ugly number. Find the nth ugly number in order from small to large. 6  * @authorWGS7  *8  */9 //This method will run out of time, M ... ..... ..... ....... ............ Ten  Public classGetuglynumber { One          Public BooleanIsuglynumber (intNumber ) { A              while(number%2==0) -number/=2; -              while(number%3==0) theNumber/=3; -              while(number%5==0) -Number/=5; -             return(number==1)?true:false; +              -         } +         //returns the nth number of ugly A          Public intGettheuglynumber (intN) { at             if(n<=0) -                 return0; -             intCount=0; -             intUglynum=0; -              while(uglynum<N) { -++count; in                 if(Isuglynumber (count)) { -uglynum++; to                 } +                  -             } the             returnCount//Nth number of ugly *              $         }Panax Notoginseng          Public Static voidMain (string[] args) { -Getuglynumber g=NewGetuglynumber (); the             intNum=g.gettheuglynumber (1500); + System.out.println (num); A              the         } +          -          $          $          -}

Solution Two:

[Train of thought] a run time-out occurs. This method is to create an array first, since each ugly number is the result of the first few ugly number * *, so save it in the array first, and then in the array
Match If the same occurs, the current index+1 will be present.

 PackageCom.exe11.offer;/*** "title" refers to numbers that contain only factors 2, 3, and 5 (Ugly number). * For example, 6, 8 are ugly numbers, but 14 is not because it contains factor 7. We used to think of 1 as the first ugly number. Find the nth ugly number in order from small to large. * [Train of thought] there is a run time-out. This method is to create an array first, since each ugly number is the result of the first few ugly numbers, so first save it in the array and then match in the array. If the same occurs, the current index+1 will be present. * @authorWGS **/ Public classGetuglynumber_solution2 { Public intGetuglynumber (intnum) {        if(num<=0)            return0; int[] uglynumbers=New int[num]; intNextuglynumberindex=1; uglynumbers[0]=1; intMultiplynumber2=0; intMultiplynumber3=0; intMultiplynumber5=0;  while(nextuglynumberindex<num) {            intMin=getmin (uglynumbers[multiplynumber2]*2, Uglynumbers[multiplynumber3]*, Uglynumbers[multiplynumber5]The); Uglynumbers[nextuglynumberindex]=min;//the smallest of the three ugly values in turn into the array 1th ... of 1500 valuesnextuglynumberindex++; if(uglynumbers[multiplynumber2]*2==min)//if the current value is just this ugly number, add amultiplynumber2++; if(uglynumbers[multiplynumber3]*3==min) MultiplyNumber3++; if(uglynumbers[multiplynumber5]*5==min) multiplyNumber5++; }        intUglynum=uglynumbers[nextuglynumberindex-1]; returnUglynum; }    Private intGetmin (intMultiplyNumber2,intMultiplyNumber3,intMultiplyNumber5) {        intMin= (MULTIPLYNUMBER2&LT;MULTIPLYNUMBER3)?Multiplynumber2:multiplynumber3; return(MIN&LT;MULTIPLYNUMBER5)?Min:multiplynumber5; }     Public Static voidMain (string[] args) {getuglynumber_solution2 g2=NewGetuglynumber_solution2 (); intN=g2.getuglynumber (1500);    SYSTEM.OUT.PRINTLN (n); }}

The sword refers to the offer series---look for the 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.