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<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