#include <iostream>
using namespace std;
int V = 5;
int F = 3;
int main()
{
void ToBinary(int num, int binary[]);
int Count1(int binary[], int &high);
int beauty[V][F] = {{7,5,-21},
{23,21,5},
{-5,-4,-4},
{-24,10,-20},
{16,23,20}};
int best_beauty = 0, best_put = 0;
int partial_sum[1 << V] = {0};
for(int i = 1; i < (1 << V); i++)
{
int binary[V] = {0};
ToBinary(i, binary);
int high;
int flowers = Count1(binary, high);
if(flowers > F)
continue;
partial_sum[i] = partial_sum[i - (1 << high)] + beauty[high][flowers - 1];
if(flowers == F && partial_sum[i] > best_beauty)
{
best_beauty = partial_sum[i];
best_put = i;
}
}
cout << "最大美感得分和:" << best_beauty << endl;
cout << "插花方法:";
int best_binary[V] = {0};
ToBinary(best_put, best_binary);
for(int vase = 0, flower = 1; vase < V; vase++)
if(best_binary[vase] == 1)
{
cout << flower;
flower++;
}
else
cout << 0;
return 0;
}
void ToBinary(int num, int binary[])
{
for(int i = 0; i < V; i++)
{
binary[i] = num & 1;
num = num >> 1;
}
}
int Count1(int binary[], int &high)
{
int count = 0;
high = -1;
for(int i = 0; i < V; i++)
if(binary[i] == 1)
{
high = i;
count++;
}
return count;
}
测试代码附带运行时的结果: