http://162.105.81.212/JudgeOnline/problem?id=1011 dfs 跟这题几乎一样poj 2362 Square #include<iostream> #include<algorithm> using namespace std; #define MAX 65 int n; int len; int parts; int max; int sum; int a[MAX]; bool used[MAX]; bool pt(int a,int b) { return a>b; } //cnt: 当前长度 //tot: 找到几段木棒了 //I:为数组下标 bool dfs(int cnt, int tot, int I) { int i; if(tot == parts) return true; for(i=I; i<n; i++) { if(i && !used[i-1] && a[i-1] == a[i]) continue; if(!used[i]) { if(cnt + a[i] < len) { used[i] = true; if(dfs(cnt+a[i], tot, i+1)) return true; used[i] = false; } else if(cnt + a[i] == len) { used[i] = true; if(dfs(0, tot+1, 0)) return true; used[i] = false; } if(cnt == 0) break; } } return false; } int main() { //freopen("in.txt", "r", stdin); int i, max; while(scanf("%d", &n), n) { sum = max = 0; for(i=0; i<n; i++) { scanf("%d", &a[i]); sum += a[i]; } sort(a, a+n, pt); max = a[0]; for(i=max; i<=sum; i++) { if(sum % i == 0) { memset(used, 0, sizeof(used)); len = i; parts = sum / i; if(dfs(0, 1, 0)) { printf("%d/n", i); break; } } } } return 0; }