原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058
任何一个数分解到最后都是由一些质数或者1相乘。基于此,题目中设一个数的质数因子在2,3,5,7之中,所以,这个数应当从1开始,不断乘以2,3,5,7获得。
最开始想到穷举法,判断一个数是否为丑数的算法如下,那依次遍历所有2~2000000000的数(第一个丑数为1),判断其是否为丑数:
//判断一个数是否为Humble Num
bool isHumble(long long k)
{
while(k%2==0)
k=k/2;
while(k%3==0)
k=k/3;
while(k%5==0)
k=k/5;
while(k%7==0)
k=k/7;
if(k==1)
return true;
else
return false;
}
刚刚我们依次遍历所有的数,再判断其是否为丑数,还考虑非丑数的情况。稍微转变一下思路,我们可以直接计算丑数,这样的话,我们就不用考虑非丑数的情况了。大致的计算过程如下,我们只需要按从小到大取前面5842个丑数即可。
AC代码1:
优先队列(最小堆,即小数在上大数在下),每次从队列中弹出一个数,再将这个数分别乘以2,乘以3,乘以5,乘以7压入队列中。
# include
# include
# define MAX 5842
using namespace std;
__int64 humble_number[MAX+1];
struct cmp1{
bool operator()(__int64 &a,__int64 &b)
{
return a>b;//最小值优先
}
};
int main()
{
int i=1,n;
humble_number[0]=0;
priority_queue<__int64,vector<__int64>,cmp1> q;
while(i<=MAX)
{
q.push(1);
while(!q.empty())
{
if(i>MAX)
break;
__int64 j=q.top();
q.pop();
if(j!=humble_number[i-1])
{
humble_number[i++]=j;
q.push(j*2);
q.push(j*3);
q.push(j*5);
q.push(j*7);
}
}
}
while(cin>>n)
{
if(n==0)
break;
else
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %d.\n",n,humble_number[n]);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %d.\n",n,humble_number[n]);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %d.\n",n,humble_number[n]);
else
printf("The %dth humble number is %d.\n",n,humble_number[n]);
}
}
return 0;
}
AC代码2(动态规划):
1
2 <- min(1*2,1*3,1*5,1*7)
3 <- min(2*2,1*3,1*5,1*7)
4 <- min(2*2,2*3,1*5,1*7)
5 <- min(3*2,2*3,1*5,1*7)
…
a <- min(num[p2]*2,num[p3]*3,num[p5]*5,num[p7]*7)
# include
# define MAX 5842
long long num[MAX+1];
long long min(long long a,long long b,long long c,long long d)
{
long long min1,min2;
min1=aif(min1<=min2)
return min1;
else
return min2;
}
int main()
{
int i=1,n;
num[1]=1;
int p2,p3,p5,p7;
p2=p3=p5=p7=1;
while(i<5842)
{
num[++i]=min(num[p2]*2,num[p3]*3,num[p5]*5,num[p7]*7);
//此处不能写为if...else if ,因为有可能num[p2]*2==num[p3]*3==num[p5]*5==num[p7]*7
if(num[i]==num[p2]*2)
p2++;
if(num[i]==num[p3]*3)
p3++;
if(num[i]==num[p5]*5)
p5++;
if(num[i]==num[p7]*7)
p7++;
}
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
else
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %lld.\n",n,num[n]);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %lld.\n",n,num[n]);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %lld.\n",n,num[n]);
else
printf("The %dth humble number is %lld.\n",n,num[n]);
}
}
return 0;
}