问题分析:
深度遍历(dfs)所有可能的组合,选择出最优组合。
组合选择条件:
1.邮票种类多的组合优先选择;
2.在邮票种类一样多的情况下,邮票张数少的组合优先选择;
3.前两个条件一样的情况下,组合中面值最大的邮票进行比较,面值更大的邮票对应的组合优先选择;
组合条件选择一个判断就够了,难点在于如何遍历得到所有可能的组合,然后再从这些组合中选择最优解。
c++代码:
如何用dfs获取所有组合?
void dfs(int n, int value) { //遍历所有合法组合
if (value > customer) return ;
if (value == customer) {
none = true;
compare();
}
if (total == 4) return ;
for (int i = n; i < types; i++) {
now[total] = i;
total++;
dfs(i, value+stamp[i]);
total--;
}
}
n是当前的邮票,value是直到当前邮票的总价值;
如果总价值大于顾客需求的价值,直接返回(剪枝);
如果总价值刚好等于顾客需求的价值,则不会输出none,且与目前已经存储的最优组合进行比较,用compare()实现;
如果当前组合的邮票张数已经是4张(题目约束条件,顾客最多拿走4张邮票),直接返回(剪枝);
继续深度遍历。
如何选择最优组合?
void compare() { //比较两个组合,选取最优组合存储
int now_types = get_type(now, total);
int end_comb_types = get_type(end_comb, end_comb_num);
int now_max = get_max(now, total);
int end_comb_max = get_max(end_comb, end_comb_num);
if (end_comb_num == 0 || (now_types > end_comb_types) || (now_types == end_comb_types && total < end_comb_num) ||
(now_types == end_comb_types && total == end_comb_num && now_max > end_comb_max)) { //判断条件
tie = false;
end_comb_num = total;
for (int i = 0; i < total; i++)
end_comb[i] = now[i];
}
if (now_types == end_comb_types && total == end_comb_num && now_max == end_comb_max) {
tie = true;
}
}
比较两个组合,end_comb[]存储最优解;
get_type功能是得到组合的邮票种类数;
get_max功能是得到组合中最大面值的邮票的面值;
完整代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int num = 4;
const int stamps = 300; //邮票种类数
bool tie, none;//由题意不言而喻
int total;//当前组合的邮票张数
int types;//每组测试(第一行)的邮票种类数
int end_comb_num;//每位顾客最终输出的组合的邮票张数
int customer;//每位顾客所需的面值
int now[num];//存储当前符合条件的组合
int end_comb[num];//存储最终结果组合
int stamp[stamps];//存储每种邮票的面值
int get_type(int a[], int n) { //获取组合的种类
if (n == 0) return 0;
int type = 1;
sort(a, a+n);
for (int i = 0; i < n-1; i++) {
if (a[i] != a[i+1])
type++;
}
return type;
}
int get_max(int a[], int n) { //获取组合中最大的那个数的值
int max = 0;
for (int i = 0; i < n; i++) {
if (max < stamp[a[i]])
max = stamp[a[i]];
}
return max;
}
void compare() { //比较两个组合,选取最优组合存储
int now_types = get_type(now, total);
int end_comb_types = get_type(end_comb, end_comb_num);
int now_max = get_max(now, total);
int end_comb_max = get_max(end_comb, end_comb_num);
if (end_comb_num == 0 || (now_types > end_comb_types) || (now_types == end_comb_types && total < end_comb_num) ||
(now_types == end_comb_types && total == end_comb_num && now_max > end_comb_max)) { //判断条件
tie = false;
end_comb_num = total;
for (int i = 0; i < total; i++)
end_comb[i] = now[i];
}
if (now_types == end_comb_types && total == end_comb_num && now_max == end_comb_max) {
tie = true;
}
}
void dfs(int n, int value) { //遍历所有合法组合
if (value > customer) return ;
if (value == customer) {
none = true;
compare();
}
if (total == 4) return ;
for (int i = n; i < types; i++) {
now[total] = i;
total++;
dfs(i, value+stamp[i]);
total--;
}
}
void print() { //输出函数
cout << customer << ' ';
if (none == false) {
cout << "---- none" << endl;
}
else {
cout << '(' << get_type(end_comb, end_comb_num) << "): ";
if (tie == true)
cout << "tie" << endl;
else {
sort(end_comb, end_comb+end_comb_num);
for (int i = 0; i < end_comb_num; i++)
cout << stamp[end_comb[i]] << ' ';
cout << endl;
}
}
}
int main() {
int n;
while (cin >> n) {
types = 0;
while (n != 0) {
stamp[types++] = n;
cin >> n;
}
sort(stamp, stamp+types);
cin >> customer;
while (customer != 0) {
total = 0;
end_comb_num = 0;
none = false;
tie = true;
dfs(0, 0);
if (none == true) {
sort(end_comb, end_comb+end_comb_num);
}
print();
cin >> customer;
}
}
return 0;
}