Ugly Number II
2015-09-05 18:46
267 查看
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
int min(int num1, int num2, int num3)
{
}
int nthUglyNumber(int n) {
}
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
int min(int num1, int num2, int num3)
{
int temp = num1 > num2 ? num2 : num1; return temp > num3 ? num3 : temp;
}
int nthUglyNumber(int n) {
if (n <= 0) return 0; int *nums = (int *)malloc(sizeof(int)*n); nums[0] = 1; int *temp2 = nums; int *temp3 = nums; int *temp5 = nums; int i = 1; while (i < n) { int temp = min(*temp2*2, *temp3*3, *temp5*5); nums[i] = temp; while(*temp2*2 <= nums[i]) temp2++; while(*temp3*3 <= nums[i]) temp3++; while(*temp5*5 <= nums[i]) temp5++; i++; } int res = nums[n-1]; free(nums); return res;
}
相关文章推荐
- [11]PHP_W3Cschool中的PHP测试题题目以及答案
- vmstat si so单位
- linux运维实战练习-2015年8月30日课程作业
- PAT Advanced 1031
- 闲聊CSS之关于clearfix--清除浮动[转]
- 排序
- 【C++】const、volatile不能修饰没有this指针的成员函数
- 使用淘宝中间件cobar实现mysql分库分表
- 研发类笔试题基础(第一篇
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
- qt 与pencv 4000 在linux下的环境搭建(一:qt平台的搭建)
- VBS中Sendkey键盘对应的码表
- 多重部分和问题
- RACCommand使用总结
- Flex中用RemoteObject与java后台通信
- (一)我的Javascript系列:Javascript的面向对象旅程(上)
- C#设计模式(2)——简单工厂模式
- 微信打飞机小游戏的尝试
- poj 2385 Apple Catching(dp)
- VS2010如何添加MSCOMM控件