Reprint please specify the source:
Viewmode=contents ">http://blog.csdn.net/u012860063?
Viewmode=contents
Topic Links:
id=1338 ">http://poj.org/problem?id=1338
Description
Ugly numbers is numbers whose only prime factors is 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
Shows the first ten ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n ' th ugly number.
Input
Each line of the input contains a postisive integer n (n <= 1500). Input is terminated by a line with n=0.
Output
For each line, output the n ' th ugly number.:D On ' t deal with the line with N=0.
Sample Input
1290
Sample Output
1210
PS: Use an array of length 1500 to store these numbers, and another three cursors, X, Y, Z;
A[1]=1. X=y=z=1, representing the first number as 1, after which the number is obtained by multiplying the number of 2,3,5,
Then x, Y, z represent a[x],a[y],a[z] can be multiplied by 2,3,5 to get the new number, I increment. Each fetch 2*a[x], 3*a[y], 5*a[z]
The minimum value in the. Get A[i] After. can move the corresponding x (or y,z) to the right, of course, assuming that the original through 3*2 to get 6, then 2*3 can also get 6,
Therefore, both X and Y may need to be incremented.
See the code:
#include <iostream>using namespace Std;int min (int a, int b, int c) {return min (a,min (b,c));} int main () {int a[1517];int x, y, z, i;x = y = z = 1, a[1] = 1;for (i = 2; I <=; i++) {A[i] = min (2*a[x],3*a[y],5*a[z ]), if (a[i] = = 2*a[x]) x++;if (a[i] = = 3*a[y]) y++;if (a[i] = = 5*a[z]) z++;} int N;while (CIN >> n && N) {Cout<<a[n]<<endl;} return 0;}
POJ 1338 Ugly Numbers (ugly number simulation)