题目描述:
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
-
输入:
-
输入包括一个整数N(1<=N<=1500)。
-
输出:
-
可能有多组测试数据,对于每组数据, 输出第N个丑数。
-
样例输入:
-
3
-
样例输出:
-
3
题目链接地址:http://ac.jobdu.com/problem.php?cid=1039&pid=17
思路:就是用已经找到的丑数乘以2,3,5继续迭代下面的,和HDU 1085(Humble numbers) 差不多#include<iostream> #include<cstdio> using namespace std; #define maxn 1501 #define min(a,b) a<b ? a:b int n,dp[maxn]; void solve() { int i,a,b,c,ta,tb,tc,t; dp[1] = 1; a = b = c = 1; for(i = 2; i < maxn; i++) { ta = dp[a] * 2; tb = dp[b] * 3; tc = dp[c] * 5; t = min(ta,tb); t = min(t,tc); if(t == ta) a++; if(t == tb) b++; if(t == tc) c++; dp[i] = t; } } int main() { //freopen("18.txt","r",stdin); solve(); while(scanf("%d",&n) != EOF) printf("%d\n",dp[n]); return 0; }