UVA136 Ugly Numbers

Source: Internet
Author: User

Problem Link: UVA136 Ugly Numbers. basic level exercises, written in C + + language program.

Test Instructions Summary: Can not be 2, 3 and 5 is not divisible by the number of primes called the ugly number, find the 1500th ugly number.

Problem analysis: In other words, the number of ugly factors can only be 2, 3, and 5. 1 is the number of ugly, for x, if x is the number of ugly 2x, 3x and 5x is the number of ugly. Using the known ugly number, from small to continuous generation of ugly number can be.

program, use an STL container set to store the number of ugly. The collection has the function of de-duplication and automatic sorting, which is convenient for solving this problem.

The AC C + + language program is as follows:

/* UVA136 Ugly Numbers * * #include <iostream> #include <cstdio> #include <set>using namespace std;# Define MAXN 1500typedef unsigned long long ull;set<ull> ugly;int main (void) {    int count;    Ugly.insert (1);    Count = 0;    Set<ull>::iterator iter = Ugly.begin ();    while (++count < MAXN) {        ULL t = *iter;        Ugly.insert (T * 2);        Ugly.insert (T * 3);        Ugly.insert (T * 5);        iter++;    }    printf ("The" th ugly number is%llu.\n ", *iter);    return 0;}


UVA136 Ugly Numbers

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.