感想:这次是借鉴别人的,写了很久都是超时,好菜啊我。。。。。
代码:http://blog.csdn.net/li4951/article/details/7434598
#include<iostream>
using namespace std;
int amount[7] = {0};
int half_value = 0;
int flag = 0;
void DFS(int value, int pre){
if(value == half_value){
flag = 1;
return;
}
if(flag == 1){ //当已经完成时,结束所有父层递归
return;
}
int i = 0;
for(i = pre; i > 0; i--){//这里i从大到小是为了快一点到half_value,剪枝效果明显一点
if(amount[i]){
if(i + value <= half_value){
amount[i]--;
DFS(i + value, i);
if(flag == 1){ //当已经完成时,结束本层的其他递归
return;
}
}
}
}
}
int main(){
int testcase = 1;
while(true){
flag = 0;
int totalvalue = 0;
int N = 6;
int i = 1;
while(i <= N){
cin >> amount[i];
totalvalue += amount[i] * i;
i++;
}
if(!totalvalue){
break;
}
printf("Collection #%d:\n", testcase++);
if(totalvalue % 2 != 0){
cout << "Can't be divided." << endl << endl;
continue;
}
half_value = totalvalue / 2;
DFS(0, 6);
if(flag){
cout << "Can be divided." << endl;
} else {
cout << "Can't be divided." << endl;
}
cout << endl;
}
return 0;
}