题目描述
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
C++11(clang++ 3.9)
class Solution { public: int GetUglyNumber_Solution(int index) { // input check if(0 == index) return 0; vector<int> ugly(index); ugly[0] = 1; int pointer_two = 0, pointer_three = 0, pointer_five = 0; for(int i = 1; i < index; i++) { ugly[i] = min(min(2 * ugly[pointer_two], 3 * ugly[pointer_three]), 5 * ugly[pointer_five]); if(ugly[i] == 2 * ugly[pointer_two] ) pointer_two++; if(ugly[i] == 3 * ugly[pointer_three]) pointer_three++; if(ugly[i] == 5 * ugly[pointer_five] ) pointer_five++; } return ugly[index - 1]; } };
原文链接: https://www.cnblogs.com/hotwater99/p/12433292.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/333945
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!