# 寻找丑数（Ugly Number）

`01``bool` `IsUgly(``int` `number)`
`02``{`
`03``    ``while``(number % 2 == 0)`
`04``        ``number /= 2;`
`05``    ``while``(number % 3 == 0)`
`06``        ``number /= 3;`
`07``    ``while``(number % 5 == 0)`
`08``        ``number /= 5;`
`09``    ``return` `(number == 1) ? ``true` `: ``false``;`
`10``}`

`01``int` `GetUglyNumber_Solution1(``int` `index)`
`02``{`
`03``    ``if``(index <= 0)`
`04``        ``return` `0;`
`05``    ``int` `number = 0;`
`06``    ``int` `uglyFound = 0;`
`07``    ``while``(uglyFound < index)`
`08``    ``{`
`09``        ``++number;`
`10``        ``if``(IsUgly(number))`
`11``        ``{`
`12``            ``++uglyFound;`
`13``        ``}`
`14``    ``}`
`15``    ``return` `number;`
`16``}`

`01``int` `GetUglyNumber_Solution2(``int` `index)`
`02``{`
`03``    ``if``(index <= 0)`
`04``        ``return` `0;`
`05``    ``int` `*pUglyNumbers = ``new` `int``[index];`
`06``    ``pUglyNumbers[0] = 1;`
`07``    ``int` `nextUglyIndex = 1;`
`08``    ``int` `*pMultiply2 = pUglyNumbers;`
`09``    ``int` `*pMultiply3 = pUglyNumbers;`
`10``    ``int` `*pMultiply5 = pUglyNumbers;`
`11``    ``while``(nextUglyIndex < index)`
`12``    ``{`
`13``        ``int` `min = Min(*pMultiply2 * 2, *pMultiply3 * 3, *pMultiply5 * 5);`
`14``        ``pUglyNumbers[nextUglyIndex] = min;`
`15``        ``while``(*pMultiply2 * 2 <= pUglyNumbers[nextUglyIndex])`
`16``            ``++pMultiply2;`
`17``        ``while``(*pMultiply3 * 3 <= pUglyNumbers[nextUglyIndex])`
`18``            ``++pMultiply3;`
`19``        ``while``(*pMultiply5 * 5 <= pUglyNumbers[nextUglyIndex])`
`20``            ``++pMultiply5;`
`21``        ``++nextUglyIndex;`
`22``    ``}`
`23``    ``int` `ugly = pUglyNumbers[nextUglyIndex - 1];`
`24``    ``delete``[] pUglyNumbers;`
`25``    ``return` `ugly;`
`26``}`
`27``int` `Min(``int` `number1, ``int` `number2, ``int` `number3)`
`28``{`
`29``    ``int` `min = (number1 < number2) ? number1 : number2;`
`30``    ``min = (min < number3) ? min : number3;`
`31``    ``return` `min;`
`32``}`