Find the nth number of ugly numbers in order from small to large

Source: Internet
Author: User
Tags min

Topic Description:

The number that contains only the factors 2, 3, and 5 is called the Ugly (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 number of ugly numbers in the order of small to large.

Input:

The input includes an integer N (1<=n<=1500).

Output:

There may be multiple sets of test data, for each set of data,

Output the nth number of ugly.

Sample input:

3

Sample output:

3

Idea: The simplest way is to determine whether the number is ugly by dividing a number by a 2,3,5. Then starting from 1, in turn, judge each number is not ugly, and write down the number of ugly, so when the number of calculations for a given value, is required to the nth number of ugly, The time complexity of this method is O (k), where the k is the size of the nth number of ugly, such as the 1500th ugly number of 859963392, then you need to judge 859,963,392 times, time efficiency is very low.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The intuitive optimization measure is to see whether the time complexity can be reduced to O (n), that is, only in the ugly number of time, rather than the number of ugly waste of time. The sword refers to the offer on the idea is very good, with O (n) of the auxiliary space to get O (n) time complexity. Its core ideas are: each ugly number must be from a previous ugly number and the product of 2,3 or 5, so that the next ugly number is used before the ugly number multiplied by 2,3,5, to find these three of the smallest and greater than the current maximum ugly number of values, that is the next requirement of the ugly number.

Note that this is tested on a nine-degree OJ, just start with the dynamic memory allocation, the result exploded WA, I tested a few sets of data, including some large data, no problem, but always WA, feel should be the reason for the dynamic memory application failure, because the basic affirmative program is not wrong, And most of the dynamic application of memory to reach 1500 * 4 bytes, and finally converted to a static array, AC, should be the test system on the heap memory problem, I can pass the compiler.

The AC code is as follows:

#include <stdio.h> #include <stdlib.h> int uglynums[1500];  
    int min (int a,int b,int c) {int min = (a<b)? a:b;  
    min = (min<c) min:c;  
return min;  
      
    int getuglynum (int index) {if (index <= 0) return 0;  
    Uglynums[0] = 1;    int currentindex= 1;  
    Subscript index int *P2 = Uglynums for the currently required number of ugliness;  
    int *p3 = uglynums;  
      
    int *p5 = uglynums;  
        Ask for each ugly number and save it while (Currentindex < index) {int min = min (2* (*P2), 3* (*P3), 5* (*P5));  
              
        Uglynums[currentindex] = min;  
        Each loop starts at the next location in the last P2,p3 and P5 while (2* (*P2) <= Uglynums[currentindex]) ++p2;  
        while (3* (*P3) <= Uglynums[currentindex]) ++p3;  
      
        while (5* (*P5) <= Uglynums[currentindex]) ++p5;  
    ++currentindex;  
    int result = Uglynums[index-1];  
return result; } int MAin () {int n;  
    while (scanf ("%d", &n)!= EOF) printf ("%d\n", Getuglynum (n));     
return 0; }

Source: http://blog.csdn.net/ns_code/article/details/27537591

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.