首页 > 代码库 > (HDU)1058 --Humble Numbers( 丑数)

(HDU)1058 --Humble Numbers( 丑数)

题目链接:http://vjudge.net/problem/HDU-1058

这题有点难度,自己写了半天依旧TLE,参考了其他人的博客。

http://blog.csdn.net/pythonfx/article/details/7292835

http://blog.csdn.net/x_iya/article/details/8774087

第二个人的博客用的是DP,放在基础题里面不大合适。

技术分享
 1 #include <stdio.h>
 2 int f[5843],n;
 3 int i,j,k,l;
 4 
 5 int min(int a,int b,int c,int d){
 6     int min=a;
 7     if(b<min) min=b;
 8     if(c<min) min=c;
 9     if(d<min) min=d;
10 
11     if(a==min) i++;
12     if(b==min) j++;
13     if(c==min) k++;
14     if(d==min) l++;
15 
16     return min;
17 }
18 
19 int main(){
20     i=j=k=l=1;
21     f[1]=1;
22     for(int t=2;t<=5842;t++)
23         f[t]=min(2*f[i],3*f[j],5*f[k],7*f[l]);
24 
25     while(scanf("%d",&n)&&n!=0){
26         if(n%10==1&&n%100!=11)
27             printf("The %dst humble number is %d.\n",n,f[n]);
28         else if(n%10==2&&n%100!=12)
29             printf("The %dnd humble number is %d.\n",n,f[n]);
30         else if(n%10==3&&n%100!=13)
31             printf("The %drd humble number is %d.\n",n,f[n]);
32         else
33             printf("The %dth humble number is %d.\n",n,f[n]);
34     }
35     return 0;
36 }
View Code
技术分享
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 long long f[6000];
 7 int main()
 8 {
 9     int n;
10     int prime[4]={2,3,5,7};
11     f[1]=1;
12     for(int i=2;i<=5842;i++)
13     {
14         f[i]=2000000001;
15         for(int j=0;j<4;j++)
16         {
17             for(int k=i-1;k>0;k--)
18             {
19                 if(f[k]*prime[j]<=f[i-1])
20                     break;
21                 if(f[k]*prime[j]<f[i])//?????????
22                     f[i]=f[k]*prime[j];
23             }
24         }
25     }
26     while(scanf("%d",&n),n)
27     {
28         if(n%10==1&&n%100!=11)
29             printf("The %dst humble number is %lld.\n",n,f[n]);
30         else if(n%10==2&&n%100!=12)
31             printf("The %dnd humble number is %lld.\n",n,f[n]);
32         else if(n%10==3&&n%100!=13)
33             printf("The %drd humble number is %lld.\n",n,f[n]);
34         else
35             printf("The %dth humble number is %lld.\n",n,f[n]);
36     }
37     return 0;
38 }
View Code

 

(HDU)1058 --Humble Numbers( 丑数)