题解 | #分割等和子集#
分割等和子集
https://www.nowcoder.com/practice/65ade309fa4d4067a9add749721bfdc0
#include <iostream> #include <cstring> using namespace std; const int N = 510; int dp[100 * N], arr[N], n; 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 { int target = sum / 2; memset(dp, 0, sizeof dp); for(int i = 0; i < n; i ++) { int val = arr[i]; dp[val] = 1; for(int j = target; j > val; j --) if(dp[j - val]) dp[j] = 1; } if(dp[target]) cout << "true"; else cout << "false"; } } // 64 位输出请用 printf("%lld")