状态转移方程:
num[p] = min(num[i]*2,num[j]*3,num[k]*5,num[l]*7);
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <iomanip> #define maxn 5900 int num[maxn]; char _th[][11] ={"th","st","nd","rd","th","th","th","th","th","th"}; using namespace std; int min_four(int a, int b, int c, int d) { int min1,min2; min1 = (a<b)?a:b; min2 = (c<d)?c:d; return (min1<min2)?min1:min2; } void set() { int i,j,k,l; i = j = k = l = 1; num[1] = 1; for(int p = 2; p <= 5842; p++) { num[p] = min_four(num[i]*2,num[j]*3,num[k]*5,num[l]*7); if(num[p]==num[i]*2)i++; if(num[p]==num[j]*3)j++; if(num[p]==num[k]*5)k++; if(num[p]==num[l]*7)l++; } } int main(int argc, char *argv[]) { int n; char s[2]; set(); while(scanf("%d",&n)&&n!=0) { if(n%100==11||n%100==12||n%100==13) s[0] = 't',s[1] = 'h'; else strcpy(s,_th[n%10]); printf("The %d%s humble number is %d.\n",n,s,num[n]); } return 0; }