Simple exercises-looking for ugly numbers

Source: Internet
Author: User
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

Summary: The number of added each step is multiplied by the previous number 2, 3, 5, and the corresponding element is linear, the corresponding position can quickly solve the next element, just pay attention to the following two points:
1. The next candidate calculated by the three probe locations will not be smaller than the current maximum (otherwise the error state), but there are 3 instances in which the same values are computed, and all the subscripts will be updated at this time;
2. The value of the increase is much faster than the feeling, before simply set a 1000000 upper limit result is exceeded, but not beyond the scope of int (1500th: 859963392)

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string> #include <map> #include <cmath> #include <assert.h> #include <vector> #include <
Limits.h> using namespace std;
 
int uglynum[1510];
    void Finduglynumber (int n) {//record the next digital subscript position to be added the int pos[3];//corresponds to 2, 3, 5 int factor[3] respectively;
    factor[0]=2,factor[1]=3,factor[2]=5;
    Uglynum[1]=1;
    int count=1;
    Pos[0]=pos[1]=pos[2]=1;
        while (count<n) {int nextnum=int_max;
        int choosed=-1; for (int i=0; i<3; i++) if (uglynum[pos[i]]*factor[i]<nextnum) {nextnum=ugly
                Num[pos[i]]*factor[i];
            Choosed=i;
        } Uglynum[++count]=nextnum;
                 
        for (int i=0; i<3; i++) if (uglynum[pos[i]]*factor[i]<=nextnum)//only possible = = pos[i]++;
    pos[choosed]++; int main () {//freopEn ("In.txt", "R", stdin);
    int n;
    Finduglynumber (1500);
         
    while (cin>>n) {cout<<uglynum[n]<<endl;
return 0;
    }/************************************************************** problem:1214 User:xjbscut language:c++ result:accepted time:50 Ms memory:1516 KB ****************************************************************/

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.