题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=325
AC代码:
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
int visited[22],ans[22],N,mix,diff,sum,total;
void dfs(int pos)
{
if(pos==N)
{
return ;
}
if(fabs(total-sum-sum)<mix)
{
mix=fabs(total-sum-sum);
}
for(int i=pos;i<N;i++)
{
if(visited[i]==0)
{
visited[i]=1;
sum+=ans[i];
dfs(i+1);
sum-=ans[i];
visited[i]=0;
}
}
}
int main()
{
while(~scanf("%d",&N))
{
memset(visited,0,sizeof(visited));
memset(ans,0,sizeof(ans));
total=0;
for(int i=0;i<N;i++)
{
scanf("%d",&ans[i]);
total+=ans[i];
}
mix=10001;
sum=0;
dfs(0);
printf("%d\n",mix);
}
return 0;
}
174

被折叠的 条评论
为什么被折叠?



