题目描述
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
int GetUglyNumber_Solution(int index) {
queue<int> q1,q2,q3;
if(index == 1) return 1;
else if(index < 1) return 0;
q1.push(2),q2.push(3),q3.push(5);
int flag = 1,temp;
while(flag < index)
{
temp = q1.front();
if(q2.front() < temp)temp = q2.front();
if(q3.front() < temp)temp = q3.front();
if(q1.front() == temp)q1.pop();
if(q2.front() == temp)q2.pop();
if(q3.front() == temp)q3.pop();
q1.push(temp*2);
q2.push(temp*3);
q3.push(temp*5);
flag++;
}
return temp;
}