201301 JAVA题目0-1级

#include<bits/stdc++.h>
using namespace std;
int Fab(int n)
{
    if(n<0)return -n;
    return n;
}
bool dfs(int i,int sum,vector<int>&v,int ans)
{
    if(i==v.size())
    {
        if(ans==sum)return true;
        else return false;
    }
    return dfs(i+1,sum+v[i],v,ans)||dfs(i+1,sum-v[i],v,ans);
}
int main()
{
    int n;
    while(cin>>n)
    {
        vector<int>v;
        int x,sum=0,sum3=0,sum5=0;
        for(int i=0; i<n; i++)
        {
            cin>>x;
            sum+=x;
            if(x==0)continue;
            else if(x%5==0)
            {
                sum5+=x;
            }
            else if(x%3==0)
            {
                sum3+=x;
            }
            else v.push_back(x);
        }
        if(sum%2)cout<<"false"<<endl;
        else if(dfs(0,0,v,Fab(sum3-sum5)))cout<<"true"<<endl;
        else cout<<"false"<<endl;
    }
    return 0;
}

你可能感兴趣的:(算法)