题解 | #数组分组#
数组分组
http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int des5_3=0;
vector<int>vec;
bool dfs(int i,int des)
{
if(i==vec.size())
{
return abs(des)==des5_3;
}
else
{
return dfs(i+1,des+vec[i])||dfs(i+1,des-vec[i]);
}
}
int main()
{
int c;
while(cin>>c)
{
vec.clear();
int sum3=0;
int sum5=0;
while(c--)
{
int data;
cin>>data;
if(data%3==0)
{
sum3+=data;
}
else if(data%5==0)
{
sum5+=data;
}
else
{
vec.push_back(data);
}
}
des5_3=abs(sum5-sum3);
if(dfs(0,0))
{
cout<<"true"<<endl;
}
else
{
cout<<"false"<<endl;
}
}
return 0;
}