51nod 2443 分成两组

分成两组
这个题应该有两种解法,递归和动态规划(01背包),
解法1:递归

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define EPS 1e-9
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define ll long long
#include 
const int MOD = 1E9+7;
const int maxn = 2e8+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
const int MaxN=20;

int n,a[25];
ll s=0,sum1=0,sum2=0,minx=inf;

void p(int x){
    if(x>n){
        sum2=s-sum1;
        minx = min(minx,abs(sum2-sum1));
        return ;
    }
    else{
        sum1+=a[x];
        p(x+1);
        sum1-=a[x];
        p(x+1);
    }
}

int main(){
    cin>>n;
    for(int i=0;i>a[i];
        s+=a[i];
    }
    p(1);
    cout<

解法2:
失败了,哈哈

你可能感兴趣的:(51nod)