请大家给一个解题的思路,看一下这个问题怎么办?

xiaokun111 2003-12-22 01:45:35
有两组数分别为:

X Y
2 5
3 2
5
4
6


将Y组的数分开分别和X组的数相加得出最佳结果,结果值相等或相近.
比如:
X组的(3+5) + Y组的(5) = 13
X组的(2+4+6) + Y组的(2) = 14

Y组的一个数可以和X组的一个或多个数相加,Y组剩下的一个数再和X组剩下的数相加,但两个相加值要相等或相近:

(3+5)+5 = 13 和 (2+4+6)+2 = 14
或:
(2+6)+5 = 13 和 (3+5+4)+2 = 14

求出最优组合。(能用SQL写出最好)
...全文
77 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xsmile 2003-12-22
  • 打赏
  • 举报
回复
x x x x x
----------- ----------- ----------- ----------- -----------
0 0 0 2 6
0 0 0 3 5
0 0 0 3 6
0 0 0 4 5
0 0 2 3 4

(5 row(s) affected)
xsmile 2003-12-22
  • 打赏
  • 举报
回复
555555想了两三个小时
还是把我的贴出来,请大家指导一下吧


select 2 as x into #temp1
select 5 as y into #temp2
insert into #temp1 select 3
union all select 4
union all select 5
union all select 6
insert into #temp2 (y) values(2)

/*
#temp1 #temp2
X Y
2 5
3 2
5
4
6
*/
select 0 as x into #temp3
insert into #temp3 select * from #temp1
union all (select * from #temp2)

declare @a as int
declare @b as int

select @a =sum(x)/2 from #temp3
select @b =(sum(x)+1)/2 from #temp3
/*判断所有数和的奇偶性*/
if (@a<>@b)
select @a=(sum(x)-1)/2,@b=(sum(x)+1)/2 from #temp3
else
select @a=sum(x)/2,@b=sum(x)/2 from #temp3

print @a
print @b

select top 1 @a-y as m ,@b-y as n into #finalresult from #temp2 order by y desc/*#finalresult 存放最后取的数据的和的两种可能性*/
/*插入0值*/
insert into #temp1 select 0

select distinct * from #temp1 A,#temp1 B,#temp1 C,#temp1 D,#temp1 E
where (A.x<B.x or (A.x=0 and B.x=0))
and (B.x<C.x or (A.x=0 and B.x=0 and C.x=0))
and (C.x<D.x or (A.x=0 and B.x=0 and C.x=0 and D.x=0))
and D.x<E.x
and (A.x+B.x+C.x+D.x+E.x in (select m from #finalresult)
or A.x+B.x+C.x+D.x+E.x in (select n from #finalresult ))

xiaokun111 2003-12-22
  • 打赏
  • 举报
回复
j9988(j9988) 我太感谢你了。
我对你的敬仰尤如滔滔江水连绵不绝!!!!
j9898 2003-12-22
  • 打赏
  • 举报
回复
while @level<=6
-->改成:
while @level<=@i

刚才粘贴错了.
j9988 2003-12-22
  • 打赏
  • 举报
回复
id ID组合
X 为X值组合
value 组合相加值
level 有几个数字相加

剩下简单了,你自已来吧。我得上班了。
j9988 2003-12-22
  • 打赏
  • 举报
回复
id X value level
-------------------- -------------------- ----------- -----------
1 2 2 1
2 3 3 1
3 4 4 1
4 5 5 1
5 6 6 1
1,2 2,3 5 2
1,3 2,4 6 2
2,3 3,4 7 2
1,4 2,5 7 2
2,4 3,5 8 2
3,4 4,5 9 2
1,5 2,6 8 2
2,5 3,6 9 2
3,5 4,6 10 2
4,5 5,6 11 2
1,2,3 2,3,4 9 3
1,2,4 2,3,5 10 3
1,3,4 2,4,5 11 3
2,3,4 3,4,5 12 3
1,2,5 2,3,6 11 3
1,3,5 2,4,6 12 3
2,3,5 3,4,6 13 3
1,4,5 2,5,6 13 3
2,4,5 3,5,6 14 3
3,4,5 4,5,6 15 3
1,2,3,4 2,3,4,5 14 4
1,2,3,5 2,3,4,6 15 4
1,2,4,5 2,3,5,6 16 4
1,3,4,5 2,4,5,6 17 4
2,3,4,5 3,4,5,6 18 4
1,2,3,4,5 2,3,4,5,6 20 5

(所影响的行数为 31 行)
j9988 2003-12-22
  • 打赏
  • 举报
回复
穷举出来:
declare @a table(id int identity,X int)
insert @a select 2
union all select 3
union all select 4
union all select 5
union all select 6

declare @level int,@i int
set @i=(select count(*) from @a)

declare @b table(id varchar(100),X varchar(100),value int,level int)
insert @b
select rtrim(id),rtrim(X),X,1 from @a
insert @b
select rtrim(A.id)+','+rtrim(B.id),rtrim(A.X)+','+rtrim(B.X),A.X+B.X,2
from @B A,@a B where cast(A.id as int)<B.id

set @level=3

while @level<=6
and exists(
select 1
from @B A,@a B
where A.level=@level-1
and cast(right(A.id,charindex(',',reverse(A.id))-1) as int)<B.id)
begin
insert @b select A.id+','+rtrim(B.id),A.X+','+rtrim(B.X),value+B.X,@level
from @B A,@a B
where A.level=@level-1
and cast(right(A.id,charindex(',',reverse(A.id))-1) as int)<B.id
set @level=@level+1
end
select * from @b order by level
xiaokun111 2003-12-22
  • 打赏
  • 举报
回复
先确定一下,如果X组的数为N,那么所有不重复组合为N的(N-1)次方
xiaokun111 2003-12-22
  • 打赏
  • 举报
回复
用SQL怎样穷举出所有X的不重复组合?
xiaokun111 2003-12-22
  • 打赏
  • 举报
回复
Y组里有几个数,就有几个结果值,但值是相差结果最小值(现在2个)
j9988 2003-12-22
  • 打赏
  • 举报
回复
算法应是这样的:
穷举出所有X的不重复组合。与Y相加,得出你要的一定范围的记录。
j9988 2003-12-22
  • 打赏
  • 举报
回复
相近是指多大范围?还是相差结果最小值
j9988 2003-12-22
  • 打赏
  • 举报
回复
是指所有符合条件的,还是找一个?
比如X(2)+Y(5) 和X(5)+Y(2)就不算?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧