逻辑还是很简单的,一个动态规划,leetcode也有相同的题目,但是不知道为什么我一开始的代码leetcode可以过,但是杭电OJ过不了,求大家解答
//OJ过不了的
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> coins = {1, 5, 10, 25, 50};
int amount;
while (cin >> amount) {
vector<long long> dp(amount + 1, 0);
dp[0] = 1;
for (int i = 0; i < coins.size(); i++) {
for (int j = coins[i]; j <= amount; j++) {
dp[j] += dp[j - coins[i]];
}
}
cout << dp[amount] << endl;
}
return 0;
};
//OJ可以过的
//来源:https://blog.csdn.net/weixin_47750287/article/details/113931419
#include<iostream>
#include<cstdio>
using namespace std;
const int COIN = 101;//硬币数量不超过100
const int MONEY = 251;//给定钱数不超过250
int dp[MONEY][COIN]={0};
int type[5]={1,5,10,25,50};
void solve()
{
dp[0][0]=1;
for(int i=0;i<5;i++){
for(int j=1;j<COIN;j++){
for(int k=type[i];k<MONEY;k++){
dp[k][j] += dp[k-type[i]][j-1];
}
}
}
}
int main()
{
int s;
int ans[MONEY]={0};
solve();//用DP计算完整的转移矩阵
for(int i=0;i<MONEY;i++){//对每个金额计算有多少种组合方案,打表
for(int j=0;j<COIN;j++){
ans[i] += dp[i][j];
}
}
while(~scanf("%d",&s)){
printf("%d\n",ans[s]);
}
return 0;
}