题解 | #数组分组#搬运工,留个记录,不是递归,我能看懂的解法

数组分组

http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

#include<bits/stdc++.h>
using namespace std;
int main(){
    int m;
    while(cin>>m){
        int sum3,sum5;
        sum3=sum5=0;
        vector<int>others;
        for(int i=0;i<m;i++){
            int temp;
            cin>>temp;
            if(temp%5==0)sum5+=temp;
            else if(temp%3==0)sum3+=temp;
            else{
                others.push_back(temp);
            }
        }
        set<int>allres;
        allres.insert(sum5-sum3);
        for(int i=0;i<others.size();i++){
            set<int> curRes;
            for(auto it=allres.begin();it!=allres.end();it++){
                curRes.insert(*it+others[i]);
                curRes.insert(*it-others[i]);
            }
            allres=curRes;
        }
        if(allres.find(0)!=allres.end()){//string 是npos
            cout<<"true"<<endl;
        }else{
            cout<<"false"<<endl;
        }
    }


}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务