|
|
вернуться в форумSame as the Coin Change Problem #include <iostream> #include <cmath> using namespace std; int minCoins(int coins[], int m, int V) { int table[V+1];
table[0] = 0;
for (int i=1; i<=V; i++) table[i] = INT_MAX;
for (int i=1; i<=V; i++) { for (int j=0; j<m; j++) if (coins[j] <= i) { int subResult = table[i-coins[j]]; if (subResult != INT_MAX && subResult + 1 < table[i]) table[i] = subResult + 1; } } return table[V]; } int main() { int V; cin>>V; int coins[1000]; int i;
for(i=1;i<=sqrt(V);i++) { coins[i-1] = (i)*(i); } int m = sqrt(V); cout<< minCoins(coins, m, V)<<endl; return 0; } This problem can be translated to get the minimum amout of coins to get the value N.(We must just check that the coins are the actual square numbers). |
|
|