题解 | #分割等和子集# dfs
分割等和子集
https://www.nowcoder.com/practice/65ade309fa4d4067a9add749721bfdc0
#include <iostream> #include <cstring> using namespace std; const int N = 510; int arr[N], n, target; bool dfs(int sum, int i) { if(sum == target) return true; if(i == n || sum > target) return false; return dfs(arr[i] + sum, i + 1) || dfs(sum, i + 1); } int main() { int sum = 0; cin >> n; for(int i = 0; i < n; i ++) { cin >> arr[i]; sum += arr[i]; } if(sum & 1) cout << "false"; else { target = sum / 2; if(dfs(0, 0)) cout << "true"; else cout << "false"; } } // 64 位输出请用 printf("%lld")