// Run time: 0.080
#include <stdio.h>
const int N = 1500;
int ugly[N] = {1, 2, 3, 4, 5};
int base[3] = {2, 3, 5};
int min(int *arr);
int main()
{
int cnt = 5;
int tmp[3];
int i, j;
int index;
while (cnt <= N)
{
for ( i = 0; i < 3; i++)
{
for ( j = 0; ugly[j] <= ugly[cnt-1]/base[i]; j++);
tmp[i] = ugly[j]*base[i];
}
index = min(tmp);
ugly[cnt++] = tmp[index];
}
printf("The 1500'th ugly number is %d.\n", ugly[N-1]);
return 0;
}
int min(int *arr)
{
int min;
min = arr[0];
if (min > arr[1])
{
min = arr[1];
if (min > arr[2])
return 2;
return 1;
}
if (min > arr[2])
return 2;
return 0;
}
沒有留言:
張貼留言