这道题用到了深度优先搜索算法,参考了下网上牛人的程序,自己调试的是正确的,提交后是Wrong Answer,不知道为什么,请大家指点:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n;
int stick[100];
int total;
int ns;
int ok;
int len;
int k;
int a[100];
int cmp(const void *a,const void *b) {
int a1 = *(int *)a;
int a2 = *(int *)b;
return a2 - a1;
}
int used[100];
int adds() {
int j = 0;
int i;
for (i = 1;i <= n;i++)
j += stick[i];
return j;
}
void search(int,int,int);
void s(int x) {
if (x > ns) {
ok = 1;
a[k] = len;
return;
}
int i;
for (i = 1;i <= n;i++)
if (!used[i]) break;
used[i] = 1;
search(x,stick[i],i);
used[i] = 0;
}
void search(int num,int now,int next) {
int i;
if (ok) return;
if (now == len) {
s(num + 1);
return;
}
if (next + 1 > n) return;
for (i = next + 1;i <= n;i++)
if (!used[i])
if(stick[i] + now <= len) {
used[i] = 1;
search(num,now + stick[i],i);
used[i] = 0;
if (ok) return;
if (stick[i] == len - now) return;
}
}
int main () {
int i;
while (scanf("%d", &n) == 1) {
if (!n) break;
ok = 0;
for (i = 1;i <= n;i++) scanf("%d", &stick[i]);
qsort(stick+1,n,sizeof(int),cmp);
total = adds();
for (i = stick[1];i <= total;i++)
if (total % i == 0 && !ok) {
ns = total / i;
memset(used,0,sizeof(used));
len = i;
s(1);
}
k++;
}
for(i=0; i<k; i++)
{
printf("%d/n", a[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n;
int stick[100];
int total;
int ns;
int ok;
int len;
int k;
int a[100];
int cmp(const void *a,const void *b) {
int a1 = *(int *)a;
int a2 = *(int *)b;
return a2 - a1;
}
int used[100];
int adds() {
int j = 0;
int i;
for (i = 1;i <= n;i++)
j += stick[i];
return j;
}
void search(int,int,int);
void s(int x) {
if (x > ns) {
ok = 1;
a[k] = len;
return;
}
int i;
for (i = 1;i <= n;i++)
if (!used[i]) break;
used[i] = 1;
search(x,stick[i],i);
used[i] = 0;
}
void search(int num,int now,int next) {
int i;
if (ok) return;
if (now == len) {
s(num + 1);
return;
}
if (next + 1 > n) return;
for (i = next + 1;i <= n;i++)
if (!used[i])
if(stick[i] + now <= len) {
used[i] = 1;
search(num,now + stick[i],i);
used[i] = 0;
if (ok) return;
if (stick[i] == len - now) return;
}
}
int main () {
int i;
while (scanf("%d", &n) == 1) {
if (!n) break;
ok = 0;
for (i = 1;i <= n;i++) scanf("%d", &stick[i]);
qsort(stick+1,n,sizeof(int),cmp);
total = adds();
for (i = stick[1];i <= total;i++)
if (total % i == 0 && !ok) {
ns = total / i;
memset(used,0,sizeof(used));
len = i;
s(1);
}
k++;
}
for(i=0; i<k; i++)
{
printf("%d/n", a[i]);
}
return 0;
}