解题思路:深搜,枚举所有情况, 只要满足枚举之和小于总重的一半,然后找出其中的最小值
# include <stdio.h>
int n;
int w[10005];
int sum;
int min;
void dfs(int t, int fsum);
int main(void)
{
while (~scanf("%d", &n))
{
int i;
sum = 0;
min = 0x3f3f3f3f;
for (i = 0; i < n; i++)
{
scanf("%d", &w[i]);
sum += w[i];
}
dfs(0, 0);
printf("%d\n", min);
}
return 0;
}
void dfs(int t, int fsum)
{
int temp = sum - 2 * fsum;
temp = temp > 0 ? temp : -temp;
if (temp < min)
{
min = temp;
}
if (t == n)
{
return ;
}
dfs(t+1, fsum+w[t]);
dfs(t+1, fsum);
}