题目描述:http://uva.onlinejudge.org/external/3/357.html
最基础的动态规划,使用滚动数组优化后代码:
/*
* 357.cc
*
* Created on: Jan 9, 2013
* Author: guixl
*/
#include <stdio.h>
#define int64 long long
int money;
int coins[5] = {1, 5, 10, 25, 50};
int64 solution[30001];
int main(int argc, char** argv) {
for (int i=0; i<5; i++)
solution[0] = 1;
for (int i=0; i<5; i++)
for (int j=1; j<30001; j++) {
solution[j] = solution[j] + (j>=coins[i] ? solution[j-coins[i]] : 0);
}
while (scanf("%d", &money) != EOF) {
if (solution[money] > 1)
printf("There are %lld ways to produce %d cents change.\n", solution[money], money);
else
printf("There is only 1 way to produce %d cents change.\n", money);
}
return 0;
}

本文介绍了解决 UVA 357 题目的动态规划方法,通过使用滚动数组优化了代码实现,提供了一个简洁高效的 C++ 示例程序。

4543

被折叠的 条评论
为什么被折叠?



