均胜电子最新招聘消息:在指定的N个数中怎样最快挑选M个,使他们的和为指定的一个数

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 01:16:12
例:230,220,530,420,750,640,720,640,310,210,720,850,290,350,490 这十五个数中,2150是哪几个数的和
例:230,220,530,420,750,640,720,640,310,210,720,850,290,350,490 这十五个数中,2150是哪几个数的和

说明:能用EXCELL 搞掂最好

const
n = 15;
total = 2150;
var
a:array[1..n] of integer =
(230,220,530,420,750,640,720,640,310,210,720,850,290,350,490);

var
b:array[1..total] of integer;
i,j:integer;
begin
for i := 1 to n
do
b[a[i]] := i;

for i := 1 to total
do
begin
for j := 1 to n
do
if (i > a[j]) and (b[i-a[j]]<>0) and (b[i-a[j]]<j) and ((b[i] = 0) or (b[i] > j))
then
b[i] := j;
end;

if (b[total] <> 0)
then
begin
i := total;
while (i <> 0)
do
begin
write(a[b[i]],' ');
i := i - a[b[i]];
end;
writeln;
end
else
writeln('No solutions!');

readln;
end.

运行结果:
750 420 530 220 230

int [] Nnum=new int[N];
int static Res;//所求的和
//快速排序,从小到大
for(int i=1;i<=N;i++){
if(Nnum[i]>=Nnum[i+1]){
X=Nnum[i];Nnum[i]=Nnum[i+1];Nnum[i+1]=X;
}
//得到N个差值
for(int i=1;i<=N;i++){
Nnum1[i]=Res-Nnum[i];
}
if(Num1[i]<0)
N1=N--;
//等于0则为结果
else if(Num1[i]=0)
outprint(Num[i]);

//得到从大到小的差值结果序列,继续求差值,可以用递
//归调用
for(i=1;i<=N1;i++)
for(j=1;j<=N;j++){
Nnum1[i]=Res-Nnum[i];
}
最后结果存Nnum1的array中,大体思路是这样,没有做过复杂度分析,具体还请楼主自己试验下吧

你的数据规模是多大?如果不大可以用穷举搜索,大的话就得动规了