一道google面试题

输入n,把1-n分成两个和相等的子集,有多少种分法

想了个dp,直接背包也行

#include <iostream>
#include <cstdio>

using namespace std;

int dp[55];

int main() {
    int n;
    scanf("%d", &n);
    int s = n * (n + 1) / 2;
    if(s & 1) {
        puts("0");
        return 0;
    }
    s >>= 1;
    dp[0] = 1;
    for(int j = 1; j <= n; j++)
        for(int i = s; i >= j; i--)
            dp[i] += dp[i-j];
    printf("%d
", dp[s] >> 1);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xiaohongmao/p/4821126.html