如何解决以下是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
这是更正的代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。