poj 1338

Ugly Numbers

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20697   Accepted: 9193

Description

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... 
shows the first 10 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 .:Don’t deal with the line with n=0.

Sample Input

1
2
9
0

Sample Output

1
2
10

Source

New Zealand 1990 Division I,UVA 136

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,dp[1600],i,j,m;
int MIN(int a,int b,int c)
{
a=min(a,b);
return min(a,c);
}
int main()
{
dp[1]=1,i=1,j=1,m=1;
for(int k=2;k<=1500;k++)
{
dp[k]=MIN(dp[i]*2,dp[j]*3,dp[m]*5);
if(dp[k]==dp[i]*2) i++;
if(dp[k]==dp[j]*3) j++;
if(dp[k]==dp[m]*5) m++;
}
while(scanf("%d",&n),n)
printf("%d\n",dp[n]);
return 0;
}

  

时间: 2024-07-31 20:18:41

poj 1338的相关文章

递推(三):POJ中的三道递推例题POJ 1664、POJ 2247和POJ 1338

[例9]放苹果(POJ 1664) Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t <= 20).以下每行均包含二个整数M和N,以空格分开.1<=M,N<=10. Output 对输入的每组数据M和N,用一行输出相应的K. Sample Input 1 7 3 Sample Output 8 (1)编程思路. 设f[m]

Ugly Numbers POJ 1338

题目链接: http://poj.org/problem?id=1338 题意 :只有素数因子 2, 3, 5 的数字才成为丑陋数字,现给出一个数字 N , 求在丑陋数字的序列里, 第N个丑陋数字是多少. 一开始想的简单粗暴, 搞个循环去存储丑陋数字了..到最后发现输入N=1500的时候, 压根什么都出不来, 已哭死.. 比赛结束后百度了一下, 才发现自己真的是单细胞生物, 找不到这个规律... ********* 规律如下:    这个丑陋数字集合是通过集合里的每一个数 × 2,× 3,× 5

poj - 1338 - Ugly Numbers(优先队列)

题意:问第n(n <= 1500)小的丑数(质因数只有2或3或5)是几. 题目链接:http://poj.org/problem?id=1338 -->>1, 2, 3, 4, 5, 6, 8, ... 假设小根堆存以上丑数,那么每次取出最小的数,这个最小的数nMin,它可以生成三个数:nMin * 2, nMin * 3, nMin * 5,将这三个数放入小根堆继续,一直复筛出1500个丑数为止. 小根堆可用优先队列来替代. #include <cstdio> #inclu

poj 1338 Ugly Numbers(丑数模拟)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338">http://poj.org/problem?id=1338 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10

51nod 1010 只包含因子2 3 5的数 &amp;&amp; poj - 1338 Ugly Numbers(打表)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中. 打表的时候要注意保持数组有序. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

poj 1338 Ugly Numbers

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21315   Accepted: 9520 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... shows the first 10 ugly num

hdu 1058 Humble Numbers || poj 1338(dp)

两题都是一样的题目 只是hdu 1058 多了个7 题意:求一个每个数因子仅含2 3 5 7 的 序列 问 第n个数是几 思路: ans[i]=min(min(ans[n2]*2,ans[n3]*3),min(ans[n5]*5,ans[n7]*7)); if(ans[i]==ans[n2]*2) n2++; if(ans[i]==ans[n3]*3) n3++; if(ans[i]==ans[n5]*5) n5++; if(ans[i]==ans[n7]*7) n7++; hdu 1058 #

POJ 1338 Ugly Numbers dp

 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21867   Accepted: 9767 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... shows the first 10 ugly

(DP) poj 1338

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21453   Accepted: 9586 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... shows the first 10 ugly num