程序代码:
#include <iostream>
using namespace std;
int len = 3;
int all_time = 26;
int fish_nums[] = {2, 1, 3};
int cost_times[] = {4, 5, 6};
int catch_num[3];
int res[3]={0,0,0};
int get_sum(int catch_num[]) {
int sum=0;
for (int i = 0; i < len; i++) {
sum+=catch_num[i]*cost_times[i];
}
return sum;
}
void catch_action(int index, int catch_num[],int all_val) {
if (index >= len) {
int sum = get_sum(catch_num);
int old_sum = get_sum(res);
if(sum <= all_val && old_sum < sum) {
for (int i = 0; i < len; i++) {
cout<<catch_num[i]<<", ";
}
cout<<endl;
for (int i = 0; i < len; i++) {
res[i]=catch_num[i];
}
}
return;
}
for (int j = 0; j <= fish_nums[index]; j++) {
catch_num[index] = j;
catch_action(index+1,catch_num,all_val);
}
}
int main() {
catch_action(0,catch_num,all_time);
cout<<"\nresult:"<<endl;
int count=0;
for (int i = 0; i < len; i++) {
cout<<res[i]<<", ";
count+=res[i];
}
cout<<"\n:"<<count<< endl;
return 0;
}
0, 0, 1,
0, 0, 2,
0, 0, 3,
0, 1, 3,
2, 0, 3,
result:
2, 0, 3,
:5