一道google面试题(dp)

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

想了个dp,直接背包也行

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 int dp[55];
 7 
 8 int main() 
 9 {
10     int n;
11     scanf("%d", &n);
12     int s = n * (n + 1) / 2;
13     if(s & 1) {//如果s为奇数,则不可能分成相等的两份 
14         puts("0");
15         return 0;
16     }
17     s >>= 1;
18     dp[0] = 1;//表示s等于0的时候,方法数为1 
19     for(int j = 1; j <= n; j++)
20         for(int i = s; i >= j; i--)
21             dp[i] += dp[i-j];
22     printf("%d
", dp[s] >> 1);
23     return 0;
24 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/5153423.html