HDOJ 1058 Humble Numbers [简单DP]

此题谨慎暴力,DP是正解。

 

#include <iostream> #include <algorithm> using namespace std; int s[5843]; int main() { int a,b,c,d; a=b=c=d=1; for(int i=1;i<8;++i) s[i]=i; for(int i=8;i<5843;++i) { while(s[a]*2<=s[i-1]) a++; while(s[b]*3<=s[i-1]) b++; while(s[c]*5<=s[i-1]) c++; while(s[d]*7<=s[i-1]) d++; s[i]=min(min(s[a]*2,s[b]*3),min(s[c]*5,s[d]*7)); } int n; while(cin>>n,n) { if(n%10==1&&n%100!=11) cout << "The "<< n <<"st humble number is "<<s[n]<<"."<<endl; else if(n%10==2&&n%100!=12) cout << "The "<< n <<"nd humble number is "<<s[n]<<"."<<endl; else if(n%10==3&&n%100!=13) cout << "The "<< n <<"rd humble number is "<<s[n]<<"."<<endl; else cout << "The "<< n <<"th humble number is "<<s[n]<<"."<<endl; } return 0; }

你可能感兴趣的:(HDOJ 1058 Humble Numbers [简单DP])