[剑指offer][面试题34]丑数
2013-10-17 16:11
363 查看
我们把只包含因子2,3,和5的数称作丑数(Ugly Number)。求按从小到大的顺序的第1500个丑数。例如6,8都是丑数,但14不是,因为它包含因子7,习惯上我们把1当做第一丑数。
#include <iostream> using namespace std; int findUglyNum(int iKth) { if (iKth<=0){ return -1; } int *piNum = new int[iKth]; piNum[0] = 1; int *piMultipler2 = piNum; int *piMultipler3 = piNum; int *piMultipler5 = piNum; int iIndexNext = 1; while (iIndexNext<iKth){ int iNumNext = min(*piMultipler2 *2, min(*piMultipler3 *3, *piMultipler5 *5)); piNum[iIndexNext++] = iNumNext; while (*piMultipler2 * 2 <= iNumNext){ piMultipler2++; } while (*piMultipler3 * 3 <= iNumNext){ piMultipler3++; } while (*piMultipler5 * 5 <= iNumNext){ piMultipler5++; } } int iUglyNum = piNum[iKth-1]; delete [] piNum; return iUglyNum; } int main() { cout<<findUglyNum(1)<<endl; cout<<findUglyNum(15)<<endl; }
相关文章推荐
- 剑指offer面试题34:丑数
- 剑指Offer_面试题34_丑数
- 剑指Offer面试题34(Java版):丑数
- 剑指offer面试题[34]丑数
- 【剑指offer 面试题34】丑数
- 剑指offer——面试题34:丑数
- 剑指Offer----面试题34:丑数
- 剑指offer——面试题34:丑数
- 剑指offer-面试题34-丑数
- 剑指offer--面试题34: 丑数
- 剑指offer 面试题34 丑数
- 【剑指offer】面试题34-丑数
- 剑指Offer面试题33把数组排成最小的数,面试题34丑数
- 剑指offer-面试题34:丑数
- 【剑指offer】5.3时间效率与空间效率的平衡——面试题34:丑数
- 剑指Offer:面试题34——丑数(java实现)
- 剑指Offer之面试题34:丑数
- 剑指Offer面试题34:丑数 Java实现
- 剑指 offer代码解析——面试题34丑数
- 剑指 offer代码解析——面试题34丑数