Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(197评价)
价格: 4431.00元
c++实现自底向上的动态规划算法有问题
thaiq发起了问答2017-07-06
0
回复
1004
浏览
int main(void) {
	int result[11];
	int p[11] = { 0,1,5,8,9,10,17,17,20,24,30};
	cout << BottomUp(0, p, result) << endl;
	cout << BottomUp(1,p,result) << endl;
	cout << BottomUp(2, p, result) << endl;
	cout << BottomUp(3, p, result) << endl;
	cout << BottomUp(4, p, result) << endl;
	cout << BottomUp(5, p, result) << endl;
	cout << BottomUp(6, p, result) << endl;
	cout << BottomUp(7, p, result) << endl;
	cout << BottomUp(8, p, result) << endl;
	cout << BottomUp(9, p, result) << endl;
	cout << BottomUp(10, p, result) << endl;
	return 0;
}
//自底向上的动态规划
int BottomUp(int n, int p[], int result[]) {
	for (int i = 1; i < n + 1; i++) {
		int tempMaxPrice = -1;
		for (int j = 1; j <= i; j++) {
			int maxPrice = p[j] + result[i - j];
			if (maxPrice > tempMaxPrice)
			{
				tempMaxPrice = maxPrice;
			}
		}
		result[i] = tempMaxPrice;
	}
	return result[n];
}

 

发表回复
你还没有登录,请先 登录或 注册!