C++题解 | #分割等和子集#

分割等和子集

http://www.nowcoder.com/practice/65ade309fa4d4067a9add749721bfdc0

c++题解

#include<bits/stdc++.h>
using namespace std;

const int N = 505;
int a[N];


int main(){
    int n, sum = 0; cin >> n;
    for (int i = 0; i < n; i++){
        cin>>a[i];
        sum += a[i];
    } 
    
    
    if (sum&1){
        cout << "false"<<endl;
        return 0;
    }
    int target = sum/2;
    vector<int> dp(target+1, 0);
    for (int i = 0; i < n; i++){
        for (int j = target; j>=a[i]; j--){
            dp[j] = max(dp[j], dp[j-a[i]]+a[i]); 
        }
    }
    if (dp[target] == target) cout<<"true"<<endl;
    else cout<<"false"<<endl;
}
全部评论

相关推荐

想进开水团喝开水:哦 给我一个 就算你真拿到牛友也会为你开心的
点赞 评论 收藏
分享
孙艹肘:校招不给三方直接让实习我都去了,,主打一个在学校呆着也是闲着,不如出来实习一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务