简单的深搜啦
#include <stdio.h> #include <stdlib.h> int sticks[100],n; bool used[100]; int cmp(const void *x,const void *y) { return *(int *)y - *(int *)x; } bool find(int left,int num,int len) { int i; if(left == 0 && num == 0) return 1; if(left == 0) left = len; for(i = 0;i < n;i ++) { if(sticks[i] <= left && !used[i]) { used[i] = 1; if(find(left - sticks[i],num-1,len)) return 1; used[i] = 0; if(sticks[i] == left || left == len) return 0; } } return 0; } int main() { int i,sum = 0; while(scanf("%d",&n) != EOF && n) { sum = 0; for(i = 0;i < n;i ++) { scanf("%d",&sticks[i]); sum += sticks[i]; used[i] = 0; } qsort(sticks,n,sizeof(int),cmp); for(i = sticks[0];i <= sum;i ++) { if((sum % i == 0) && find(i,n,i)) { printf("%d\n",i); break; } } } return 0; }