呵呵呵,这题的话,去年不知道怎么就水过去了,现在做还是懵逼了
总是感觉这题很奇怪,哎
2,3,5,7的系数必然在已打出的表中取
状态转移方程
dp(n) = min(dp[i]*2,dp[j]*3,dp[k]*5,dp[l]*7)
i<=j<=k<=l<n,
a[4]={2,3,5,7}
用一个一维数组保存下标,cnt[i]记录a[i]所能取得的最大表下标
1.遍历所有的cnt[i]找最小值
mmin为dp[cnt[i]*a[i]]中的最小值
2.遍历所有的cnt[i],更新cnt[i]
if dp[cnt[i]]*a[i]==mmin
cnt[i]++;
//5842 #include <iostream> #include<cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <string> using namespace std; const int inf = (1<<31)-1 ; const int MAXN = 6e3; int dp[MAXN]; int a[4]={2,3,5,7}; int ct[4]={1,1,1,1}; int main() { int n; int mmin; dp[1] = 1; for(int i=2;i<=5842;i++){ mmin = inf; for(int j=0;j<4;j++){ mmin = min(mmin,a[j]*dp[ct[j]]); } dp[i] = mmin; for(int j=0;j<4;j++){ if(dp[i]==a[j]*dp[ct[j]]) ct[j]++; } } while(scanf("%d",&n),n){ cout<<"The "<<n; if(10<n&&n<20)cout<<"th "; else if(n%10==1)cout<<"st "; else if(n%10==2)cout<<"nd "; else if(n%10==3)cout<<"rd "; else cout<<"th "; cout<<"humble number is "<<dp[n]<<"."<<endl; } //cout << "Hello world!" << endl; return 0; }
时间: 2024-10-09 08:07:02