这个。。。我没用DP做,直接打表然后sort了 = =。。
后来CG小盆友给我讲的DP做法,应该是每次乘过一个因子,那么这个因子对应的数就向前走一步,下次计算的时候就直接拿对应的最小数去乘以对应的因子,找到最小值。
DP方法网上一堆,我就不写了。
打表方法。
#include <queue> #include <stack> #include <math.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <string> #include <algorithm> #define MID(x,y) ( ( x + y ) >> 1 ) #define L(x) ( x << 1 ) #define R(x) ( x << 1 | 1 ) #define BUG puts("here!!!") using namespace std; const int MAX = 6000; const int MAX_N = 31; const int inf = 2000000000+50; int dp[MAX]; int main() { memset(dp,0,sizeof(dp)); int cnt = 1; for(int i=0; i<31; i++) for(int j=0; j<20; j++) for(int k=0; k<20; k++) for(int p=0; p<20; p++) { double tmp = pow(2.0,i)*pow(3.0,j)*pow(5.0,k)*pow(7.0,p); if( tmp < inf ) dp[cnt++] = (int)tmp; } sort(dp,dp+cnt); int n; while( ~scanf("%d",&n) && n ) { printf("The %d",n); if( n % 10 == 1 && n % 100 != 11 ) printf("st"); else if( n % 10 == 2 && n % 100 != 12 ) printf("nd"); else if( n % 10 == 3 && n % 100 != 13 ) printf("rd"); else printf("th"); printf(" humble number is %d.\n",dp[n]); } return 0; }