Sword refers to offer series source code-ugly number

Source: Internet
Author: User

Topic 1214: Ugly time limit: 1 seconds Memory limit: 32 Mega Special sentence: No submission: 1700 Resolution: 756 Title Description: The number that contains only the factors 2, 3, and 5 is called an ugly number (Ugly numbers). 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. Input: The input includes an integer N (1<=n<=1500). Output: There may be more than one set of test data, for each set of data, output nth number of ugly. Sample input: 3 Sample output: 3

#include <iostream> #include <stdio.h>using namespace std;int min (int a,int b,int c) {int Min = (a<b)? a:b; return MIN&LT;C?MIN:C;}    int getuglynumber (int index) {if (index<=0) {return 0;    } int * puglynumbers = new Int[index];    Puglynumbers[0] = 1;    int nextuglyindex = 1;    int* pMultiply2 = puglynumbers;    int* pMultiply3 = puglynumbers;    int* pMultiply5 = puglynumbers;        while (nextuglyindex<index) {int min = min (*pmultiply2*2,*pmultiply3*3,*pmultiply5*5);        Puglynumbers[nextuglyindex] = min;        while (*pmultiply2*2<=puglynumbers[nextuglyindex]) {++pmultiply2;        } while (*pmultiply3*3<=puglynumbers[nextuglyindex]) {++pmultiply3;        } while (*pmultiply5*5<=puglynumbers[nextuglyindex]) {++pmultiply5;    } nextuglyindex++; } return puglynumbers[nextuglyindex-1];}    int main () {int n; while (scanf ("%d", &n)!=eof) {printf ("%d\n", Getuglynumber (n)); } return 0;}

Sword refers to offer series source code-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.