主要解决如何以最小的粒度向前推进。
#include <iostream>
using namespace std;
#define min(x, y) (x)<(y)?(x):(y)
int getUglyNumber(int index)
{
if(index<0)
return 1;
int* nums = new int[index];
nums[0] = 1;
int next_id = 1;
int* ugly2 = nums;
int* ugly3 = nums;
int* ugly5 = nums;
while(next_id<index)
{
nums[next_id] = min(*ugly2*2, min(*ugly3*3, *ugly5*5));
while(*ugly2*2<=nums[next_id])
ugly2++;
while(*ugly3*3<=nums[next_id])
ugly3++;
while(*ugly5*5<=nums[next_id])
ugly5++;
next_id++;
}
int ret = nums[--next_id];
delete []nums;
return ret;
}
int main()
{
cout<<getUglyNumber(10)<<endl;
return 0;
}