以下是gfg中子集和问题的代码请告诉我里面有什么问题

问题描述

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool check(int a[],int n,int s)
{
    bool dp[n+1][s+1];
    for(int i=0;i<n+1;i++)
    {
        for(int j=0;j<s+1;j++)
        {
            if(i==0)
                dp[i][j] = false;
            if(j==0)
                dp[i][j] = true;
            if(a[i-1]<=j)
                dp[i][j] = dp[i-1][j-a[i-1]] || dp[i-1][j];
            else
                dp[i][j] = dp[i-1][j];
        }
    }
    return dp[n][s];
}
int main()
 {
    //code
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        int s=0;
        for(int i=0;i<n;i++){
            cin>>arr[i];
            s+=arr[i];
        }
        if(s%2==0)
        {
            s/=2;
            if(check(arr,n,s))
                cout<<"YES\n";
            else
                cout<<"NO\n";
        }
        else
            cout<<"NO\n";
    }
    return 0;
}

问题是 给定一组数字,请检查是否可以将其划分为两个子集,以使两个子集中的元素之和相等。

对于一个测试用例; 4 1 5 11 5 输出为是,因为 存在两个子集,例如{1、5、5}和{11}。

上述测试用例通过了,但显示错误的答案 8 479 758 315 472 730 101 460 619 实际答案应该为“是”,但显示为“否”。

解决方法

diana

这是更正的代码。