#include <iostream>
#include <stdlib.h>
using namespace std;
int Getuglynumber(int index);
int Min(int number1,int number2,int number3);
int main(){
int index;
cin>>index;
int result = Getuglynumber(index);
cout<<result<<endl;
return 0;
}
int Min(int number1,int number2,int number3){
int min = (number1<number2)?number1:number2;
min = (min<number3)?min:number3;
return min;
}
int Getuglynumber(int index){
if(index<=0) return 0;
int *puglynumbers = new int[index];
puglynumbers[0] = 1;
int nextuglyindex = 1;
int *pmultiply2 = puglynumbers;
int *pmultiply3 = puglynumbers;
int *pmultiply5 = puglynumbers;
while(nextuglyindex < index){
int min = Min(*pmultiply2*2,*pmultiply3*3,*pmultiply5*5);
puglynumbers[nextuglyindex] = min;
while(*pmultiply2*2<=puglynumbers[nextuglyindex])
++pmultiply2;
while(*pmultiply3*3<=puglynumbers[nextuglyindex])
++pmultiply3;
while(*pmultiply5*5<=puglynumbers[nextuglyindex])
++pmultiply5;
++nextuglyindex;
}
int ugly = puglynumbers[nextuglyindex-1];
delete[] puglynumbers;
return ugly;
}
丑数(空间换时间)
最新推荐文章于 2020-02-04 12:07:34 发布