#include <string>
#include <iostream>
#include <ctime>
using namespace std;
int price_count = 6;
bool record[20] = {0};
int max_price(int *price, int length)
{
if (length == 1) return price[1];
if (length == 0) return 0;
int max = (length <= price_count && length >= 1) ? price[length] : 0;
if (record[length])
return price[length];
int temp = 0, single = 0;
for (int i = 1; i < length; ++i)
{
temp = max_price(price, i) + max_price(price, length - i);
if (temp > max)
max = temp;
}
price[length] = max;
record[length] = 1;
return max;
}
int main(int argc, char const *argv[])
{
int price[] = {0, 1, 5, 8, 9, 10, 17};
for (int i = 20; i >= 8; --i)
cout << max_price(price, i) << endl;
cout << clock() << endl;
return 0;
}
#include <iostream>
#include <ctime>
using namespace std;
int price_count = 6;
bool record[20] = {0};
int max_price(int *price, int length)
{
if (length == 1) return price[1];
if (length == 0) return 0;
int max = (length <= price_count && length >= 1) ? price[length] : 0;
if (record[length])
return price[length];
int temp = 0, single = 0;
for (int i = 1; i < length; ++i)
{
temp = max_price(price, i) + max_price(price, length - i);
if (temp > max)
max = temp;
}
price[length] = max;
record[length] = 1;
return max;
}
/*
* max_price = max{max_price(i) + max_price(j)}
* 对特定长度进行递归,把已计算过的中间最大值进行存储指表,
* 便于下次递归使用
*/int main(int argc, char const *argv[])
{
int price[] = {0, 1, 5, 8, 9, 10, 17};
for (int i = 20; i >= 8; --i)
cout << max_price(price, i) << endl;
cout << clock() << endl;
return 0;
}