/* 题目: 求从1开始的第n个丑数。 */ /* 思路: 按顺序列出各个丑数。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; int GetUglyNumber_Solution(int index) { if(index <= 0) return 0; int uglyNumbers[index]; uglyNumbers[0] = 1; int *pMultiply2 = uglyNumbers; int *pMultiply3 = uglyNumbers; int *pMultiply5 = uglyNumbers; int next = 1; while(next < index){ uglyNumbers[next++] = min(min((*pMultiply2)*2,(*pMultiply3)*3),(*pMultiply5)*5); while((*pMultiply2)*2 <= uglyNumbers[next-1]){ pMultiply2++; } while((*pMultiply3)*3 <= uglyNumbers[next-1]){ pMultiply3++; } while((*pMultiply5)*5 <= uglyNumbers[next-1]){ pMultiply5++; } } return uglyNumbers[index-1]; } int main(){ cout<<GetUglyNumber_Solution(6); return 0; }