标签:
2 3 -1
2 3
题解:这个方法解决丑数非常实用;
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<vector> 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int MAXN=5010; 11 typedef long long LL; 12 LL ans[MAXN]; 13 LL MIN(LL x,LL y,LL z){ 14 LL d=x; 15 if(y<d)d=y; 16 if(z<d)d=z; 17 return d; 18 } 19 int main(){ 20 int n,x_2=1,x_3=1,x_5=1; 21 LL d; 22 ans[1]=1; 23 int i=1; 24 while(i<=5000){ 25 i++; 26 d=MIN(ans[x_2]*2,ans[x_3]*3,ans[x_5]*5); 27 ans[i]=d; 28 if(d==ans[x_2]*2)x_2++; 29 if(d==ans[x_3]*3)x_3++; 30 if(d==ans[x_5]*5)x_5++; 31 } 32 while(scanf("%d",&n),n>0)printf("%lld\n",ans[n]); 33 return 0; 34 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4959958.html