研究了两个晚上,还是没有想出什么好的办法,用递归写了个程序,用组合公式算了算,仅仅32个数的和的情况就有3e20个结果,如果再算上从32个到45个计算量不可想像,以下是算32个数的和的程序(10分钟1轮都没走完- -,放弃):
#include "stdio.h"
#define number 171780
long sum[72];
int *p[72];
int num=32; /*定义求多少个数的和*/
int a_width=72; /*定义数组宽度*/
int data[]={4816,4776,4498,4616,4948,4684,4710,5266,4760,5168,4770,5134,5076,4784,
5174,4732,4782,4746,4730,5224,5164,4742,4724,4730,4762,4162,4188,4830,4942,4072,
4270,4520,4808,5130,4272,4262,4238,4240,4828,4168,4754,4720,4896,4824,5206,4322,
3800,4826,4632,5104,5134,4838,3858,5008,4828,5234,4324,5282,5348,5150,4724,4682,
4720,5218,4744,4742,4760,5194,4792,4712,4754,4704};
int comb(int x);
main()
{
int max,min,i;
int num_max,num_min;
max=min=data[0];
for(i=1;i<72;i++)
{
if(max<data[i])
max=data[i];
if(min>data[i])
min=data[i];
}
printf("Max:%d,Min:%d\n",max,min);
num_max=number/min;
num_min=number/max;
printf("num_max:%d,num_min:%d\n",num_max,num_min);
for(i=0;i<72;i++)
{
sum[i]=0;
}
printf("Finding,please wait...\n");
comb(num);
}
comb(int x)
{
int i;
if(x==1)
{
for(p[x-1]=p[x]+1;p[x-1]<(data+a_width);p[x-1]++)
{
sum[x-1]=sum[x]+*p[x-1];
/*for(i=0;i<num-1;i++)
{
printf("\t");
}
printf("%d=%d\n",*p[x-1],sum[x-1]);*/
printf("%ld\t",sum[x-1]);
if(sum[x-1]==number)
{
for(i=num;i>0;i--)
{
printf("%d+",*p[i]);
}
printf("=%ld",number); /*如果找到所求在这里输出*/
}
sum[x-1]=0;
}
}
else if(x==num)
{
for(p[x-1]=data;p[x-1]<=(data+a_width-num);p[x-1]++)
{
sum[x-1]=sum[x]+*p[x-1];
/*printf("%d+\n",*p[x-1]);*/
comb(x-1);
printf("\nThe number %d can be exclude!\n",*p[x-1]);
sum[x-1]=0;
}
}
else
{
for(p[x-1]=p[x]+1;p[x-1]<=(data+a_width-x);p[x-1]++)
{
sum[x-1]=sum[x]+*p[x-1];
/*for(i=0;i<num-x;i++)
{
printf("\t");
}
printf("%d+\n",*p[x-1]);*/
comb(x-1);
sum[x-1]=0;
}
}
}