解题思路:
(1)递归
#include<iostream>
#include<vector>
using namespace std;
bool tag=false;
void judge(vector<int> &v,int start,int target3,int target5) {
if(target3==0 && target5==0) tag=true;
else if(start<v.size()) {
judge(v,start+1,target3-v[start],target5);
judge(v,start+1,target3,target5-v[start]);
}
}
bool help(vector<int> &v) {
vector<int> v1;
int sum3=0,sum5=0,sum=0;
for(int i=0;i<v.size();i++) {
if(v[i]%3==0 && v[i]%5!=0) sum3+=v[i];
else if(v[i]%3!=0 && v[i]%5==0) sum5+=v[i];
else {
v1.push_back(v[i]);
sum+=v[i];
}
}
sum+=sum3;
sum+=sum5;
if(sum/2*2!=sum) return false;
judge(v1,0,sum/2-sum3,sum/2-sum5);
if(tag==true) return true;
else return false;
}
int main() {
int n,a;
while(cin>>n) {
tag=false;
vector<int> v;
for(int i=0;i<n;i++) {
cin>>a;
v.push_back(a);
}
if(help(v)) cout<<"true"<<endl;
else cout<<"false"<<endl;
}
return 0;
}