POJ 1338 Ugly Numbers 寻找丑数
2011-02-15 23:10
525 查看
动态规划,比较简单,直接参见下面的代码。
![](http://hi.csdn.net/attachment/201102/15/8847609_12977825859ww1.png)
![](http://hi.csdn.net/attachment/201102/15/8847609_12977825859ww1.png)
/*Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13272 Accepted: 5856 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 <stdio.h> #define MAX_NUM_OF_UNGLY_NUMBER 1501 int gaiUnglyNum[MAX_NUM_OF_UNGLY_NUMBER]; #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define THREE_MIN(a,b,c) MIN(MIN(a,b),c) int UglyNumbersmain(void) { int iLoop; int iLoop2 = 1; int iLoop3 = 1; int iLoop5 = 1; int iNthUgly; gaiUnglyNum[1] = 1; for (iLoop = 2; iLoop < MAX_NUM_OF_UNGLY_NUMBER; iLoop++) { while (gaiUnglyNum[iLoop2]*2 <= gaiUnglyNum[iLoop-1]) { iLoop2++; } while (gaiUnglyNum[iLoop3]*3 <= gaiUnglyNum[iLoop-1]) { iLoop3++; } while (gaiUnglyNum[iLoop5]*5 <= gaiUnglyNum[iLoop-1]) { iLoop5++; } gaiUnglyNum[iLoop] = THREE_MIN(gaiUnglyNum[iLoop2]*2,gaiUnglyNum[iLoop3]*3,gaiUnglyNum[iLoop5]*5); } while(1) { scanf("%d",&iNthUgly); if (0 == iNthUgly) { break; } printf("%d/n",gaiUnglyNum[iNthUgly]); } return 0; }
相关文章推荐
- POJ1338 Ugly Numbers 丑数 [数论]
- (DS1.5.8)POJ 1338 Ugly Numbers(寻找因子只含有2||3||5的数)
- POJ-1338-Ugly Numbers-丑数
- poj 1338 Ugly Numbers [ 离线预处理 - 只处理丑数,复杂度很低 此题也是 程序猿的经典面试题]
- POJ 1338 Ugly Numbers(我的水题之路——丑数2,3,5,质因子组成数)
- POJ 1338 Ugly Numbers(丑数)
- poj 1338 Ugly Numbers(丑数模拟)
- POJ 1338 Ugly Numbers(丑数)
- poj 1338 Ugly Numbers(丑数模拟)
- POJ-1338 Ugly Numbers 打表 Or 递推
- poj1338 Ugly Numbers(技巧)
- poj1338——Ugly Numbers
- POJ-1338 Ugly Numbers 递推
- POJ1338——丑数
- poj 1338 Ugly Numbers 枚举素因子幂
- poj - 1338 - Ugly Numbers(优先队列)
- 37.寻找丑数[Ugly numbers]
- Ugly Numbers--POJ 1338
- poj 1338 ugly numbers和poj 2247Humble Numbers 解题报告(附详细分析)
- POJ 1338 Ugly Numbers