“硬币问题”经典的记忆化搜索。注意找的是有多少种类。而不是最少用多少硬币。
将正常的算法改为2维的即可。。。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int Case, d[7500][6];
int V[6] = { 0, 1, 5, 10, 25, 50 };
int dp ( int S, int x ) {
if ( d[S][x] >= 0 ) return d[S][x];
d[S][x] = 0;
for ( int i = x; i <= 5; ++i ) {
if ( S >= V[i] )
d[S][x] += dp ( S - V[i], i );
}
return d[S][x];
}
int main ( ) {
memset ( d, -1, sizeof ( d ) );
for ( int i = 0; i <= 5; d[0][i] = 1, ++i ) ;
while ( cin >> Case ) {
cout << dp ( Case, 1 ) << endl;
}
}