题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
#include<iostream> #include<vector> #include<math.h> #include<algorithm> #include<numeric> using namespace std; bool dfs(vector<int>& nums,int bag1,int bag2,int idx){ if(idx==nums.size()){ if(bag1==bag2) return true; else return false; } return dfs(nums,bag1+nums[idx],bag2,idx+1)||dfs(nums,bag1,bag2+nums[idx],idx+1); } int main(){ int n; cin>>n; vector<int> nums; int x; int bag1=0,bag2=0; for(int i=0;i<n;i++){ cin>>x; if(x%3==0){ bag1+=x; }else if(x%5==0){ bag2+=x; }else{ nums.push_back(x); } } bool res=dfs(nums,bag1,bag2,0); if(res) cout<<"true"; else cout<<"false"; system("pause"); }